Batch file quirk I'd love to fix

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Arcath
Posts: 2
Joined: 13 Aug 2012 14:59

Batch file quirk I'd love to fix

#1 Post by Arcath » 14 Aug 2012 00:09

So I have a script that I use occasionally to rename files. I'll get a bunch of files on a usb drive, I move them to a specific directory and then I run the script to add a prefix to the files and then move them into storage. The problem is that for some reason the first file always gets the prefix added twice. Here is an example and then the script:

    log1325.txt
    log1326.txt
    log1327.txt
    log1328.txt

I run the script and enter 'PC135-' and this is what I get

    PC135-PC135-log1325.txt
    PC135-log1326.txt
    PC135-log1327.txt
    PC135-log1328.txt

Code: Select all

@echo off
echo "Enter new filename (No spaces!)"
set /p NewName=
for %%i in (*.txt) do move %%i %NewName%_%%i
echo *Press any key to move these files now*
pause
move *.txt w:\Files\


I added the pause at the end to give me a chance to fix the file before it gets archived. When I'm dealing with 30+ files at a time I don't mind fixing the first file, but I'd figure I'd ask in case there was a quick fix.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file quirk I'd love to fix

#2 Post by foxidrive » 14 Aug 2012 01:16

The problem is a bug in the for in do command. This style will fix the issue.

Code: Select all

@echo off
echo "Enter new filename (No spaces!)"
set /p NewName=
for /f "delims=" %%i in ('dir *.txt /b') do move %%i %NewName%_%%i
echo *Press any key to move these files now*
pause
move *.txt w:\Files\

Arcath
Posts: 2
Joined: 13 Aug 2012 14:59

Re: Batch file quirk I'd love to fix

#3 Post by Arcath » 14 Aug 2012 06:32

That worked perfectly, thank you foxidrive.

Post Reply