MID function for batch file
I came back with a question about the package. I asked this question about getting ranges in a batch file so that we can transfer backups more easily. After the file has been copied into our system, we will unzip the file so that we have a "working backup" of our client data. Now I am trying to move the file that was just deleted from the current directory to the 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 folder AlreadyUnzipped
contains subfolders labeled by year. Year folders have subfolders 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 unpacked. What I would like to do is when the decompression process is complete, I would like the file to be moved to the appropriate archive folder. An example would be:
daily20140105.zipx
need to go to
H:\benton_off_site_backup\zipbackups\AlreadyUnzipped\2014\1.January
I did some research on the internet and found this website but couldn't get anywhere. I am doing the MID equivalent. If I need to explain myself even more, then I will gladly do it. Thanks in advance!
UPDATE
Many thanks to @David Ruhmann for all his help! He brought 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 it repeats.
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 they do nothing! It only extracts the file and then says the system cannot find the path and moves on to the next file to extract. Grrrr!
COMPLETED:
I am very grateful to @DavidRuhmann and @Magoo. Without their help and keen understanding, I would be stuck spinning my wheels, waiting for an answer. I used both of my inputs for my last 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, give these guys love by hacking their input. Thanks again!
source to share
Something like this should do what you asked.
@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
source to share
The only problem the error ran into is purely "air code" (theoretical and untested)
(noting also that it for
has an unclosed parenthesis - possibly a crash and dropout)
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 for testing are for ECHO
ed only . After you have confirmed that the commands are correct , change ECHO(MD
to MD
to create directories. Add 2>nul
to suppress error messages (for example, when a directory already exists)
For testing purposes, the required MOVE commands are simply ECHO
ed. Once you've confirmed the commands are correct , change ECHO(MOVE
to MOVE
to actually move the files. Add >nul
to suppress report messages (for example 1 file moved
)
further thoughts:
Of course, the decomposition is faulty.
CALL SET "monthdir=!filename:~-8,4!\!mm!.%%month[!mm!]%%"
must work. This is for me.
Meanwhile - two lines ECHO
ed:
Additional MD
existing directory won't hurt. Call it a security measure if you like; omit it if you like. As I have documented, if you delete echo(
and append 2>nul
then the error message generated for the existing directory will be displayed.
Finally, I believe that the cause of the "cannot find file" message may be the terminal \
in the command MOVE
- I was under the impression that the target was a directory specification, not a file specification, so 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 )
source to share