Name: jan (by janita) Date: March 13, 2008 at 09:50:29 Pacific Subject: batch script to replace filenames h OS: windows 2000 CPU/Ram: 512 Model/Manufacturer: vectra 400
Comment:
Hi,
I need to replace filenames having spaces and dots with underscre using a .bat script
example:
need to convert filename like RBS Diff data we 06.07.07.xls to RBS_Diff_data_we_06_07_07.xls
Run the following batch in the folder holding the files to be renamed
@echo off setlocal enabledelayedexpansion for %%j in (*.*) do ( set filename=%%~nj set filename=!filename=.=_! set filename=!filename= =_! if not !filename!==%%~nj ren %%j !filename!%%~xj )
This is right now, sorry again and I hope you had no harm
@echo off setlocal enabledelayedexpansion for %%j in (*.*) do ( set filename=%%~nj set filename=!filename:.=_! set filename=!filename: =_! if not !filename!==%%~nj ren %%j !filename!%%~xj )
Usually I don't release code without carefully testing it before, but this time I was very busy and too confident in myself. So the first miscoded script and now another bug.
Replace
if not !filename!==%%~nj ren %%j !filename!%%~xj
with
if not "!filename!"=="%%~nj" ren "%%j" "!filename!%%~xj"
Actually I needed to change these file names bcoz I had to find if these files were in certain folder and if so either do some processing or delete them.
But when spaces and dots are present I cudnt use the wildcard * to find files starting with a particular name bcoz after the space or dot it does not recognise the filename and always says file not found. I tried enclosing in quotes as well
So thought of renaming and continue with my script. Is this a fine idea? or do u have a simpler solution of finding filenames with spaces and dots.
Below is the deletion script i wrote for any filename starting as SSL. It works fine if there are no dots and spaces. Otherwise not!!
@ECHO OFF SET PATH="C:\DEL\Program Files\Data\BCL\Processed" SET FILENAME=SSL SET FILELIMIT=20
if exist %PATH%\%FILENAME%*.txt ( For /F "TOKENS=1,2* DELIMS=[]" %%A IN ('DIR %PATH%\%FILENAME%*.txt /B /O:-D /T:C ^| find /N /I ".txt"') Do IF %%A GTR %FILELIMIT% DEL /F %PATH%\%%B
here the working (I hope fine) version of the script you posted. You messed up with quotes and never use a reserved system word for your own variables (PATH).
@Echo Off
Set myPath=C:\DEL\Program Files\Data\BCL\Processed Set FileName=SSL Set FileLimit=20
If exist "%myPath%\%FileName%*.txt" ( Echo. For /F "tokens=1,2 delims=[]" %%A in ('Dir "%myPath%\%FileName%*.txt" /B /O:-D /T:C ^| Find /N /I ".txt"') Do ( If %%A gtr %FileLimit% ( Del /F "%myPath%\%%B" Echo. File "%%B" Cleared on: %Date% at: %Time% >> "%myPath%\BCL_Deletion.log" Echo. File "%%B" Cleared on: %Date% at: %Time% ) ) )
Set myPath= Set FileName= Set FileLimit= :: End_Of_Batch
If you need more support in developing your scripts, contact me freely using e-maail too. Ever glad to help when time allows.
are you sure that more than 20 files with name starting SSL exist in your directory?
I carefully tested my script and it always worked fime either with names embedding dots or spaces and with a directory structure exactly duplicating what you posted. Just lowered the FileLimit to two for testing purposes.
So, please, verify what I said above to help me if bugs have to be fixed.
The problem you face is due to the weird char & that in NT batch has the special meaning of joining to commands on the same line.
In minutes I post the slightly modified code of the script to work around that. Be a bit patient because it is a chinese work to set up the changes required.
Here the modified code that enables dynamic variables marked with ! instead of the conventional % and uses the caret (^) to deactivate special & symbol inside variables. Beware I have not tested it.
@Echo Off SetLocal EnableDelayedExpansion
Set IFACEPATH=C:\DEL\Program Files\Accurate NXG\S^&M_Data_Surplus Set FILENAME=SLM Set FILELIMIT=2
Echo "!IFACEPATH!\!FILENAME!*.csv" If exist "!IFACEPATH!\!FILENAME!*.csv" ( For /F "tokens=1,2 delims=[]" %%A in ('Dir "!IFACEPATH!\!FILENAME!*.csv" /B /O:-D /T:W ^| Find /N /I ".csv"') Do ( If %%A gtr !FILELIMIT! ( Del /F "!IFACEPATH!\%%B" Echo. File "%%B" Cleared on: %Date% at: %Time% >> "!IFACEPATH!\S&M_Data_Surplus_Deletion.log" Echo. File "%%B" Cleared on: %Date% at: %Time% ) ) ) Echo. ********************************************************************************* >> "!IFACEPATH!\S&M_Data_Surplus_Deletion.log" :: End_Of_Batch
The above solution has worked except that I replaced the ! with the % bcoz with ! it din work.
Thank you very much for the solution.
I have a final trouble with my code. With the working logic you provided I tried to write scripts to delete all files (any file name and any extension) from the errors folder I have.
This doesnt seem to work.
Plz find below the code I use.
@ECHO OFF
Set IFACEPATH=C:\DEL\Program Files\Errors Set FILELIMIT=2
echo "%IFACEPATH%\*.*" If exist "%IFACEPATH%\*.*" ( For /F "tokens=1,2 delims=[]" %%A in ('Dir "%IFACEPATH%\*.*" /B /O:-D /T:W ^| Find /N /I ".*"') Do ( If %%A gtr %FILELIMIT% ( Del /F "%IFACEPATH%\%%B" Echo. File "%%B" Cleared on: %Date% at: %Time% >> "%IFACEPATH%\Errors_Deletion.log" Echo. File "%%B" Cleared on: %Date% at: %Time% ) ) ) Echo. ********************************************************************************* >> "%IFACEPATH%\Errors_Deletion.log"
Set IFACEPATH= Set FILENAME= Set FILELIMIT= :: End_Of_Batch
The above solution has worked except that I replaced the ! with the % bcoz with ! it din work.
Thank you very much for the solution.
I have a final trouble with my code. With the working logic you provided I tried to write scripts to delete all files (any file name and any extension) from the errors folder I have.
This doesnt seem to work.
Plz find below the code I use.
@ECHO OFF
Set IFACEPATH=C:\DEL\Program Files\Errors Set FILELIMIT=2
echo "%IFACEPATH%\*.*" If exist "%IFACEPATH%\*.*" ( For /F "tokens=1,2 delims=[]" %%A in ('Dir "%IFACEPATH%\*.*" /B /O:-D /T:W ^| Find /N /I ".*"') Do ( If %%A gtr %FILELIMIT% ( Del /F "%IFACEPATH%\%%B" Echo. File "%%B" Cleared on: %Date% at: %Time% >> "%IFACEPATH%\Errors_Deletion.log" Echo. File "%%B" Cleared on: %Date% at: %Time% ) ) ) Echo. ********************************************************************************* >> "%IFACEPATH%\Errors_Deletion.log"
Set IFACEPATH= Set FILENAME= Set FILELIMIT= :: End_Of_Batch
the switch /A:-D-H in the DIR command should solve the extra folder issue. The following code takes into account the number of files deleted too, but for the date and time I need you give me your local format as date and time are country dependent (in Italy we use DD/MM/YYYY HH.MM.SS in 24h format not at all the US format). So please post how the script displays and I will give you the layout you need.
Sorry today I'm at customer's company so I can't replay for the whole working day. If you post what I need, Friday you'll find date and time formatted.
Have a nice day
@ECHO OFF Set IFACEPATH=C:\DEL\Program Files\Errors Set FILELIMIT=2 Set COUNT=0
Echo "%IFACEPATH%\*.*"
If exist "%IFACEPATH%\*.*" ( For /F "tokens=1,2 delims=[]" %%A in ('Dir "%IFACEPATH%\*.*" /B /A:-D-H /O:-D /T:W ^| Find /N /V ""') Do ( If %%A gtr %FILELIMIT% ( Del /F "%IFACEPATH%\%%B" Echo. File "%%B" Cleared on: %Date% at: %Time% >> "%IFACEPATH%\Errors_Deletion.log" Echo. File "%%B" Cleared on: %Date% at: %Time% Set /A COUNT+=1 ) ) ) Echo.*** %date% - %time% : %COUNT% file(s) deleted *** >> "%IFACEPATH%\Errors_Deletion.log" Echo.*** %date% - %time% : %COUNT% file(s) deleted ***
Set IFACEPATH= Set FILENAME= Set FILELIMIT= Set COUNT= :: End_Of_Batch
The information on Computing.Net is the opinions of its users. Such
opinions may not be accurate and they are to be used at your own risk.
Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE