Modify existing batch script to rename files

October 18, 2012 at 12:28:34
Specs: Windows 7
Need some help with my current batch script. It takes files named like AB12-00234_1, AB12-00234_2, DC14-00333_1, etc. and first creates a copy to another directory. It then renames the files with a -A. So, they become AB12-00234-A_1, AB12-00234-A_2, etc. In my situation, I need to be able to say, "IF first 2 characters of filename begin with DC, DX, AA, do not rename. Any help on this would be hugely appreciated! See below:

cd /d C:\mydirectory1
Rem copy files first
xcopy C:\mydirectory1\*.* \\mydirectory2\
rem rename files
@for /f "tokens=1-4 delims=-_." %%a in ('dir /b') do ren %%a-%%b_%%c.%%d %%a-%%b-A_%%c.%%d
:: end

rem move to new dir
move C:\mydirectory1\*.* C:\mydirectory3\


See More: Modify existing batch script to rename files

Report •


#1
October 18, 2012 at 17:15:02
This might work, but it's probably not the cleanest solution:
::====== begin script
cd /d C:\mydirectory1
Rem copy files first
xcopy C:\mydirectory1\*.* \\mydirectory2\
rem protect AA, DC, DX files from renaming
call :hide +
rem rename files
@for /f "tokens=1-4 delims=-_." %%a in ('dir /b') do ren %%a-%%b_%%c.%%d %%a-%%b-A_%%c.%%d

rem bring back the protected files
call :hide -
rem move to new dir
move C:\mydirectory1\*.* C:\mydirectory3\
goto :eof

:hide
attrib %1h AB??-????_*.*
attrib %1h DC??-????_*.*
attrib %1h DX??-????_*.*
::===== end
not tested


Report •

#2
October 19, 2012 at 13:09:38
Hmm...it's just copying the files into the directories and not renaming any of them.

Report •

#3
October 19, 2012 at 16:49:18
Well, it should have worked, from what I tested on my end. Try inserting these debugging lines imm. after the "call :hide +" statement:
attrib | more
pause
and see what things look like. (ie, if ALL of your files have "H"'s in front of their names, then something is amiss.) On mine, only the three qualifying files were hidden. Also, you might temporarily remove the @ from in front of the for-loop and watch the output. Maybe something there got spiked. But I'm betting it's the ":hide" subroutine, although it does not appear to fail in my test. I never got the files extensions. If they're all the same, then what is it? Maybe a more specific extension (my test filenames have no extensions).

Report •

Related Solutions

#4
October 24, 2012 at 11:11:26
It looks like the rename portion of the script that's not working. If I have files in the directory named AB12-00123_1.jpg, PS12-12365_1.jpg in my directory, the files just get moved to the new directories. The "protected" prefix (AB) should not have been renamed but the PS prefix should have been renamed to PS12-12365-A_1.jpg. The "-A" gets added to the name.

Report •

#5
October 24, 2012 at 16:56:48
Well, that part (renaming) was "inherited". I just reiterated it verbatim, so if it was working before, I don't know what i could have done to make it fail. It has some extra variables that aren't really needed however. Try this batch snippet and see what comes out:

@echo off & setlocal
dir /b ????-????_*.*
pause > nul | echo these files should qualify for renaming...
:: this does NOT rename, it only shows the command trying to do so.
for /f "tokens=1* delims=_" %%a in ('dir /b ????-????_*.*') do echo ren %%a_%%b %%a-A_%%b
::== end

I don't know what else to tell you. If this doesn't shed light, I'm clueless, but other forum contributors might solve the problem. (repost if necessary).


Report •


Ask Question