This question already has answers here:
How to replace all spaces by underscores in all file names of a folder?
(8 answers)
Closed 7 years ago.
I have searched on this forum but not been able to recognize what I am looking for.
IRC dosen't allow the DCC of the files having spaces. How to create a batch file which replace spaces with underscore for all the MP3/FLAC files in the folder.
I tried this but failed
#echo off
Setlocal enabledelayedexpansion
Set "Pattern= "
Set "Replace=_"
For %%a in (*.mp3) Do (
Set "File=%%~a"
Ren "%%a" "!File:%Pattern%=%Replace%!"
)
Pause&Exit
It's giving me the error that the files are being used by another process though the files were not used by any programs and now it ain't doing anything whenever I run it it just says "press any key to continue and shuts down"
Any help?
thanks.
Check with echo in front of rename command. It should works.
But if you are getting the files are being used by another process. This is certainly the case.
This can be an application blocked on closing, application hidden in systray, windows search indexer.
Restart or logoff computer may solve.
#echo off
setlocal enabledelayedexpansion
set "pattern= "
set "replace=_"
for %%a in (*.mp3) do (
set "file=%%~nxa"
echo Ren "%%~fa" "!file:%pattern%=%replace%!"
::pause
)
pause
exit /b 0
Related
This question already has answers here:
Variables are not behaving as expected
(1 answer)
Example of delayed expansion in batch file
(5 answers)
Closed 2 years ago.
I have some folders that are named a specific way:
Administration
LS01....
LS02....
LS03....
I want to create a batch file that is located in that folder. When the batch file is started, it goes through the names of the folders and changes the LS to SW, uses the numbers and removes the excess name.
I have no experience with batch-coding, but tried my best. This is what I could come up with until now:
for /D %%f in ("%cd%\*") do (
set "name=%%~nf"
set name=%name:LS=%
rename "%%f" "SW_%name%"
pause.
)
I don't understand why the setting of the variable name does not work. The folders get renamed but only with SW_. The number, which should be in the variable name, does not show up.
Could you please help me out with my code ?
Thank you for your attention.
Best regards
Sam
PS: I'm new to stackoverflow
You need delayedexpansion:
#echo off
setlocal enabledelayedexpansion
for /D %%f in ("%cd%\*") do (
set "name=%%~nf"
set name=!name:LS_=!
rename "%%~f" "SW_!name!"
pause.
)
even better is to skip the second set replacement:
#echo off
setlocal enabledelayedexpansion
for /D %%f in ("%cd%\*") do (
set "name=%%~nf"
rename "%%~f" "!name:LS=SW!"
pause.
)
Additionally. You are setting the name of the variable to the filename only, not the extension included, this will rename the file to exclude the extension, is this your intensions? If not, change %%~nf to %%~f
In order read more about delayedexpansion, see set /? and setlocal /? from cmd.exe.
I'm dabbling with a batch script in which I'm trying to run a command if the string read from a text file matches the local string. I managed to get the file reading part working but I can't get the IF condition to work and the IF crashes the batch script but when I remove it works.
My Batch Code,
#ECHO OFF
SET content=
SetLocal EnableDelayedExpansion
FOR /f "tokens=* usebackq delims=" %%p in (file.txt) do SET content=!content!%%p
IF "%content%" == "Doit"(
ECHO boy it's working
PAUSE
) ELSE (
ECHO Not working
PAUSE
)
EndLocal
I'm primarily a PHP programmer not much experience with Batch scripting. Hope this is not a duplicate because I did several searches before posting this. Any way please show me how to get this done.
This question already has an answer here:
for loop working in CMD prompt but not in batch file - for loop was copy pasted
(1 answer)
Closed 3 years ago.
I'm kinda a bit of a noob to this stuff. I'm trying to make a batch script to convert Backup Exec 10 xml log files to text.
I got some great answers from here when searching, cooked up a script and have tried to run it. It bombs at the first "for" statement and I'm not sure why.
I can run every step of this script manually and it works great. But if I double click on the batch file and run it, after the second pause it bombs.
Anyone see anything out of place here? I'm at my wits end. I think the "for" statement might need tweaking. I've been messing around but haven't found the right combo for it to run successfully in the script.
#echo off
echo Starting Backup To Text SCript...
pause
cd c:\Program Files\Symantec\Backup Exec\Data
echo Get the latest BEX_TAPEBACKUP File...
pause
FOR /F "usebackq" %f in (`dir /Od /B`) do set "FILE=%f"
echo %FILE% will be converted to text
pause
cd c:\Program Files\Symantec\Backup Exec
REM This command will take the current Backup Exec XML log file and convert it to text.
pause
bemcmd -o31 -l"C:\Users\Administrator\Desktop\Backup Logs\backuplog.txt" s0 - f"C:\Program Files\Symantec\Backup Exec\Data\%FILE%"
pause
echo Done
Use the right syntax for your for loop:
FOR /F "delims=" %%f in ('dir /od /B') do set "FILE=%%~f"
You need to double your %'s in the For.
FOR /F "usebackq" %%f in (`dir /Od /B`) do set "FILE=%%f"
This question already has an answer here:
At which point does `for` or `for /R` enumerate the directory (tree)?
(1 answer)
Closed 3 years ago.
I'm using a windows batch file to rename a very large number of files in one go. The renaming simply adds a prefix to the file name. The file executes fine except for the last file, to which the prefix is somehow applied twice. I'm not sure what I'm doing wrong. Can anyone point out the problem to me?
for %%i in (*.csv) do ren %%i myprefix_%%i
#echo off
SETLOCAL
DEL *.csv
XCOPY ..\*.csv .
(
for %%i in (*.csv) do (
echo ====================
echo %%i
dir *.csv
ren %%i x%%i
dir *.csv
echo ====================
)
)>u:\junk.txt&edit u:\junk.txt
I ran the above batch in a clean directory below a directory that contained a few .CSVs. All it does is zap the current directory's .csvs, copy those from the parent directory and then rename by prefixing. It revealed quite a bit.
(U: is a RAMDRIVE, EDIT executes EDITPLUS; NOTEPAD would do as well here)
The operation depends on quite what prefix is added. A prefix starting "a" works differently from a prefix starting "x". AND the result depends on the filesystem being used. NTFS presents the filenames alphabetically but the FAT system on the RAMDRIVE doesn't sort the names.
I believe the problem is caused by the 'findnext' filename mechanism not dealing as expected with a changing list of filenames. As it changes one, that name may move in the list and hence the file may be reprocessed as its new name is encountered again - possibly multiple times.
A cure is to replace the for selection with
for /f "delims=" %%i in ('dir /b *.csv') do (
where the DIR list is created and THEN processed, so the changes don't affect it.
You may be having some spaces in your filenames. Use double-quotes around filenames. Try the following:
for %%i in (*.csv) do ren "%%I" "myprefix_%%I"
This will execute as:
ren "tmp - Copy.csv" "myprefix_tmp - Copy.csv"
Also, you should look for any hidden files.
How would I achieve this:
for i in *.e; do mv $i ${i%-b*.e}.e; done
in a Windows batch file? (It renames files containing "-b" to the part before "-b". Note that this is not necessarily the end of the string! e.g. "file-b-4.e" will become "file.e")
If you really want to do this in batch, this should work
#echo off
setlocal disableDelayedExpansion
for %%F in (*.e) do (
set "var=%%~F"
setlocal enableDelayedExpansion
set "var=!var:-b=.e:!"
for /f "eol=: delims=:" %%A in ("!var!") do (
endlocal
echo ren "%%F" "%%A"
)
)
Edit
The comment by panda-34 alluded to the fact that the original posted code failed if the file name begins with -b. The code above was fixed by incorporating the extension into the replacement string. (thanks panda-34 for alerting me to the problem)
panda-34 also provided an alternate solution that uses command injection with search and replace. The injected command is the REM statement.
The panda-34 solution works as long as the file name does not contain & or ^ characters, but fails if it does.
Below is a modified version of the command injection technique that should work with all valid Windows file names. There are 2 critical mods, 1) make sure the special chars in the file name are always quoted, and 2) do not pass the value as a CALL argument, otherwise ^ will be doubled to ^^.
#echo off
setlocal disableDelayedExpansion
for %%i in (*-b*.e) do (
set old="%%~ni"
call :ren_b
)
exit /b
:ren_b
set v=%old:-b=.e"&rem "%
echo ren "%old:~1,-1%.e" %v%
exit /b
Final Edit (I hope):
As baruch indicates in his comment, the solutions above remove starting with the 1st occurance, whereas the original bash command removes starting with the last occurance.
Below is a version that should be an exact equivalent of the original bash command.
#echo off
setlocal disableDelayedExpansion
set "search=-b"
for %%A in (*%search%*.e) do (
set "old=%%A"
setlocal enableDelayedExpansion
set "new=\_!old:%search%=\_!"
for %%B in ("!new!") do (
endlocal
set "new=%%~pB"
setlocal enableDelayedExpansion
set "new=!new:~2,-1!.e"
echo ren "!old!" "!new:\_=%search%!"
endlocal
)
)
Simple, really
for %%i in (*-b*.e) do call :ren_b %%~ni
goto :eof
:ren_b
set v=%*
set v="%v:-b=.e" ^& rem %
ren "%*.e" %v%
Here's a variant to keep the name till the last -b occurence
setlocal enabledelayedexpansion
for %%i in (*-b*.e) do (
set v=%%~ni
set v=!v:-b=\!
for %%j in ("\!v!") do (
set v=%%~pj
set v=!v:~1,-1!
set v=!v:\=-b!
ren "%%i" "!v!.e"
)
)
It will fail for names containing ! and starting with -b.
P.S, Didn't see, dbenham already provided the equivalent solution, probably with more provisions for terminal cases of file names.
Forget it, some convenient things cannot be done in NT scripting. What you are asking here is not possible to my knowledge. And I've written and maintained complex NT scripts bigger than 50 KiB, using all kinds of tricks. The book "Windows NT Shell Scripting" points out many of these, for the same and more see Rob van der Woude's scripting pages.
I reckon you could do part of this, but certainly not in a one-liner due to how variable expansion works in NT scripting. For example you could extract the part of the string that you expect to be -b and check whether it is -b, then extract the other parts and rename from the original name to the one that is comprised of only the extracted parts.
But you'll likely need ten to fifteen lines to achieve that. In that light, consider using a different scripting language for the purpose. Especially if this is a modern Windows version.
I realize this is not the desired answer (i.e. that this is possible and a sample), but cmd.exe is very limited compared to Bash, albeit by far not as limited as some opponents of traditional batch scripting are pointing out.