Hi,
I have some files in the directory "A".
a.txt
b.txt
c.txt
d.txt
z.txt
master_yyyymmdd.txt
I want to move the files (a,b,c,d,z) to the directory "History" based on master_yyyymmdd file.
for example,
after the move, the files will be a_20090804.txt , b_20090804.txt, c_20090804.txt,...etc
Is that possible?
how to manipulate the file names
Moderator: DosItHelp
-
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
Code: Select all
@echo off
setlocal enabledelayedexpansion
set "folder=c:\path\to\your\files"
::First extract the date portion of the master_yyyymmdd.txt file
::It MUST be in that format.
for /f "tokens=2 delims=_." %%a in ('dir "%folder%\master_*"') do set "date=%%~a"
::Next we get the filenames in the directory, make sure it's not the master_yyyymmdd.txt file,
::and then move them into the history folder with the new name.
for /f %%b in ('dir /b /a-d "%folder%"') do (
set "filename=%%~b"
if not "!filename:~0,6!"=="master" move "%folder%\%%~b" "%folder%\history\%%~nb_%date%%%~xb"
)
just a little change..
My user came back to me and said they want to rename the master file to
"Master_mm_yyyy.xls"
I changed the syntax a little and works now.
for /f "tokens=2,3 delims=_." %%a in ('dir "%folder%\master_*"') do set "date=%%~a_%%~b"
Except master_* is case-sensitive. so It moved Master_* file to history directory. not sure how to ignore the case sensitive though.
My user came back to me and said they want to rename the master file to
"Master_mm_yyyy.xls"
I changed the syntax a little and works now.
for /f "tokens=2,3 delims=_." %%a in ('dir "%folder%\master_*"') do set "date=%%~a_%%~b"
Except master_* is case-sensitive. so It moved Master_* file to history directory. not sure how to ignore the case sensitive though.
-
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
Oops -- need /i on the if statement like this:
Code: Select all
if /i not "!filename:~0,6!"=="master" move "%folder%\%%~b" "%folder%\history\%%~nb_%date%%%~xb"
w
why you want a super code why don't you use a simple rename??????
-
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
Re: w
Because there might be thousands of files, and also presumably the master_yyyymmdd.txt file will change often. One piece of code will take care of both things, otherwise you have to manually enter multiple move commands.rfpd wrote:why you want a super code why don't you use a simple rename??????