简体   繁体   中英

MID function for Batch file

I'm back with a batch file question. I asked this question about getting ranges in a batch file so we can more easily bring over backups. Once the file has been copied to our system, we unzip the file so we have a "running backup" of our client's data. What I'm trying to accomplish now is to move the file that was just been unzipped from its current directory to an archive directory. So far I have the following:

@echo off
FOR /F %%i IN (H:\benton_off_site_backup\zipbackups\ZipBackupsList.txt) DO (
c:\wzzip\wzunzip -o -d H:\benton_off_site_backup\zipbackups\%%i h:\benton_off_site_backup\
move H:\benton_off_site_backup\zipbackups\%%i H:\benton_off_site_backup\zipbackups\AlreadyUnzipped
)
exit

This is fine, but the AlreadyUnzipped folder contains sub-folders labeled by the year. The year folders have sub-folders labeled by month.

i.e. H:\benton_off_site_backup\zipbackups\AlreadyUnzipped\2014\1.January
     H:\benton_off_site_backup\zipbackups\AlreadyUnzipped\2014\2.February
     ...

These folders contain everything that has been unzipped. What I would like to happen is once the unzipping process has been completed, I would like the file to be moved to it's corresponding archive folder. An example would be:

daily20140105.zipx

needs to go to

H:\benton_off_site_backup\zipbackups\AlreadyUnzipped\2014\1.January

I've done some research online and found this website, but have been unable to get anything to happen. I've been doing the MID equivalent. If I need to further explain myself, then I will gladly do so. Thanks in advance!

UPDATE

Much thanks to @David Ruhmann for all his help! His got me to this point

setlocal EnableDelayedExpansion
set "count=0"
for %%A in (January February March April May June July August September October November December) do set /a "count+=1" & set "month[!count!]=%%A"

FOR /F %%i IN (H:\benton_off_site_backup\zipbackups\ZipBackupsList.txt) DO (
c:\wzzip\wzunzip -o -d H:\benton_off_site_backup\zipbackups\%%i h:\benton_off_site_backup\

set "filename=%%~ni"
set "mm=!filename:~-4,2!"
if "!filename:~-4,1!"=="0" set "mm=!filename:~-3,1!"
call move H:\benton_off_site_backup\zipbackups\%%~i H:\benton_off_site_backup\zipbackups\AlreadyUnzipped\!filename:~-8,4!\!mm!.%%month[!mm!]%%\%%i

When I debug it, the following is echoed it out.

move H:\benton_off_site_backup\zipbackups\daily20140920.zipx
H:\benton_off_site_backup\zipbackups\AlreadyUnzipped\2014\9.September\daily20140920.zipx

The paths are correct, but it doesn't do anything! It only extracts the file, then says the system can't find the path and moves to the next file to be extracted. GRRRR!

FINISHED:

I'm so very grateful to @DavidRuhmann and @Magoo. Without their help and keen insight, I would be stuck spinning my wheels in vein pursuit of an answer. I've used both of their input for my final program which is what follows:

@echo off

setlocal EnableDelayedExpansion
set "count=0"
for %%A in (January February March April May June July August September October November December) do set /a "count+=1" & set "month[!count!]=%%A"

FOR /F %%i IN (H:\benton_off_site_backup\zipbackups\ZipBackupsList.txt) DO (
 c:\wzzip\wzunzip -o -d H:\benton_off_site_backup\zipbackups\%%i h:\benton_off_site_backup\

 set "filename=%%~ni"
 set "mm=!filename:~-4,2!"
 if "!filename:~-4,1!"=="0" set "mm=!filename:~-3,1!"
 CALL SET "monthdir=!filename:~-8,4!\!mm!.%%month[!mm!]%%"
 MD H:\benton_off_site_backup\zipbackups\AlreadyUnzipped\!monthdir! 2>NUL
 call move H:\benton_off_site_backup\zipbackups\%%~i H:\benton_off_site_backup\zipbackups\AlreadyUnzipped\!monthdir!\%%i

)
endlocal
exit

If you're still reading this, go give these guys some love by up-voting their contributions. Thank you again!

Something like this should do what you requested.

@echo off

setlocal EnableDelayedExpansion
set "count=0"
for %%A in (January February March April May June July August September October November December) do set /a "count+=1" & set "month[!count!]=%%A"

FOR /F %%i IN (H:\benton_off_site_backup\zipbackups\ZipBackupsList.txt) DO (
c:\wzzip\wzunzip -o -d H:\benton_off_site_backup\zipbackups\%%i h:\benton_off_site_backup\

set "filename=%%~ni"
set "mm=!filename:~-4,2!"
if "!filename:~-4,1!"=="0" set "mm=!filename:~-3,1!"
call move "H:\benton_off_site_backup\zipbackups\%%~i" "H:\benton_off_site_backup\zipbackups\AlreadyUnzipped\!filename:~-8,4!\!mm!.%%month[!mm!]%%\%%~i"

)
endlocal
exit

Solely addressing the error now encountered - and this is purely "air code" (theoretical and untested)

(noting also that the for has an unclosed parenthesis - probably a cut-and-paste glitch)

FOR /F %%i IN (H:\benton_off_site_backup\zipbackups\ZipBackupsList.txt) DO (
 c:\wzzip\wzunzip -o -d H:\benton_off_site_backup\zipbackups\%%i h:\benton_off_site_backup\

 set "filename=%%~ni"
 set "mm=!filename:~-4,2!"
 if "!filename:~-4,1!"=="0" set "mm=!filename:~-3,1!"
 SET "monthdir=!filename:~-8,4!\!mm!.%month[!mm!]%"
 echo(MD H:\benton_off_site_backup\zipbackups\AlreadyUnzipped\!monthdir!
 ECHO(move H:\benton_off_site_backup\zipbackups\%%~i H:\benton_off_site_backup\zipbackups\AlreadyUnzipped\!monthdir!\%%i\
)

The required MD commands are merely ECHO ed for testing purposes. After you've verified that the commands are correct , change ECHO(MD to MD to actually create the directories. Append 2>nul to suppress error messages (eg. when the directory already exists)

The required MOVE commands are merely ECHO ed for testing purposes. After you've verified that the commands are correct , change ECHO(MOVE to MOVE to actually move the files. Append >nul to suppress report messages (eg. 1 file moved )


further thoughts:

Certainly, the expansion is faulty.

 CALL SET "monthdir=!filename:~-8,4!\!mm!.%%month[!mm!]%%"

should work. It does for me.

Meanwhile - the two echo ed lines:

The extra MD of an existing directory will do no harm. Call it a safety measure if you like; omit it if you like. As I documented, if you remove the echo( and append 2>nul then the error message generated for an existing directory will be suppressed.

Finally, I believe that the cause of the 'can't find the file' message may be the terminal \\ in the move command - I was under the impression that the destination was a directory specification, not a file specification, so that terminal \\ should be omitted.

Revised batch segment:

FOR /F %%i IN (H:\benton_off_site_backup\zipbackups\ZipBackupsList.txt) DO (
 c:\wzzip\wzunzip -o -d H:\benton_off_site_backup\zipbackups\%%i h:\benton_off_site_backup\

 set "filename=%%~ni"
 set "mm=!filename:~-4,2!"
 if "!filename:~-4,1!"=="0" set "mm=!filename:~-3,1!"
 CALL SET "monthdir=!filename:~-8,4!\!mm!.%%month[!mm!]%%"
 echo(MD H:\benton_off_site_backup\zipbackups\AlreadyUnzipped\!monthdir!
 ECHO(move H:\benton_off_site_backup\zipbackups\%%~i H:\benton_off_site_backup\zipbackups\AlreadyUnzipped\!monthdir!\%%i
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM