How to create bat file that zip all folders using winzip? - windows

I want to create a bat file that compresses all the folders inside a specific directory and then deletes them.
For example,
Folder structure (before running bat file):
VidoesArch
January
February
March
....
TextualArch
January
February
March
....
Folder structure (after running bat file):
VideosArch
January.zip
February.zip
March.zip
....
TextualArch
January.zip
February.zip
March.zip
....
This what I have developed so far:
#echo off
setlocal
set INPBKP=C:\Data\Backup\VideosArch
set OUTBKP=C:\Data\Backup\TextualArch
set WZLOC=C:\PROGRA~2\WINZIP
:: Caution: Use a drive where sufficient disk space is available for this setting.
set WORKDIR=K:\
:: DO NOT CHANGE ANYTHING BEYOND THIS POINT
set COMPCMD="%WZLOC%\WZZIP.EXE" -m -ex -b%WORKDIR% -a
for /F "USEBACKQ DELIMS==" %%t in (`dir %INPBKP%\*.dat /a/s/b`) do #%COMPCMD% %%t.zip %%t
for /F "USEBACKQ DELIMS==" %%t in (`dir %OUTBKP%\*.dat /a/s/b`) do #%COMPCMD% %%t.zip %%t
endlocal
What this file does is, it zip all the .dat files in the folders. What I want to achieve is it shall zip all the folders.
Update:
I have changed the file content and now it looks like this. Its not working. If I try to run on command prompt it says- 'The system cannot find the file specified'.
#echo off
setlocal
set INPBKP=C:\Data\Backup\VideosArch
set OUTBKP=C:\Data\Backup\TextualArch
set WZLOC=C:\PROGRA~2\WINZIP
:: Caution: Use a drive where sufficient disk space is available for this setting.
set WORKDIR=C:\
:: DO NOT CHANGE ANYTHING BEYOND THIS POINT
set COMPCMD="%WZLOC%\WZZIP64.EXE" -r -p -m -ex -b%WORKDIR%
for %%s in ("%INPBKP%" "%OUTBKP%") do for /F "DELIMS==" %%t in ('dir %%s /ad /b') do %COMPCMD% %%~nt.zip "%%~s\%%t"
endlocal

set COMPCMD="%WZLOC%\WZZIP.EXE" -r -p -m -ex -b%WORKDIR%
for /F "DELIMS==" %%t in ('dir %INPBKP% /ad /b') do %COMPCMD% "%INPBKP%\%%~nt.zip" %INPBKP%\%%t
Adding -r -p to the compress command should gather and retain all the files and directories.
By executing dir /b /ad, %%t will be assigned the directorynames from the target directory. %%~nt selects simply the name part. The usebackq is not required if single quotes are used.
You could possibly also use
for %%s in ("%INPBKP%" "%OUTBKP%") do for /F "DELIMS=" %%t in ('dir /ad /b %%s') do %COMPCMD% "%%~s\%%~nt.zip" "%%~s\%%t"
%%s containing the quoted directoryname and %%~s unquoted (in case of separators in directorynames)
I haven't tested this, so I'd suggest you try it on some disposible test directories first.
[edit - to insert the destination directory for the zip file]
[also changed delims== to delims= (cosmetic - change = as delimiter to no delimiter)]

Related

Source file name for output

I am trying to create a batch file using PDFtk to burst combine file in a certain directory to the output folder using the source file name (which can vary) as the input file name. for example
source directory :- D:\Temp\IN
destination directory :- D:\Temp\OUT
File name :- abcdefgh.pdf (which can vary)
desired output file name :- abcdefgh-001.pdf, abcdefgh-002.pdf and so on
My batch file will reside in D:\Script
The PDFtk.exe is in D:\PDFtk Server\bin
I tried for 1 whole day I can't get the input filename for the output.
Can anyone help
My existing program :-
CD D:\Temp\IN
for /f "tokens=*" %%A in ('dir /b D:\Temp\IN\*.pdf') do (
set prefix=%%~ni )
set outname=%prefix%-%%03d-00.pdf
path D:\PDFtk Server\bin
pdftk.exe D:\Temp\IN\%prefix% burst output D:\Temp\OUT\%outname%
exit
There are multiple errors in your approach the first one is that in a case like this you need local delayed expansion. For an initial % later use it as !.
CD D:\Temp\IN
This is ok but would be better if it allowed for /Drive shifting to "quoted drive folder" (beware input folder must NOT in this case be same as output folder with this approach, it is best if output is a sibling as you have done or use a common parent is better.)
CD /D "D:\Temp\IN"
for /f "tokens=*" %%A in ('dir /b D:\Temp\IN\*.pdf') do (
set prefix=%%~ni )
You have mixed up %%A and %%i they should be the same, tokens is not needed in this case but usebackq is for odd filenames and add quotes, don't need current directory, and ) is way too early.
setlocal enabledelayedexpansion
for /f "usebackq delims==" %%A in (`dir /b *.pdf`) do (
set "prefix=%%~nA"
next part
set outname=%prefix%-%%03d-00.pdf
again best quoted and needs using expansion
set "outname=!prefix!-%%03d-00.pdf"
path D:\PDFtk Server\bin
pdftk.exe D:\Temp\IN\%prefix% burst output D:\Temp\OUT\%outname%
better if combined and quoted you don't need to use Current Directory
"D:\PDFtk Server\bin\pdftk.exe" "!prefix!.pdf" burst output "D:\Temp\OUT\!outname!"
the terminal bracket goes here
)
exit
is superfluous
so all in all
CD /D "D:\Temp\IN"
setlocal enabledelayedexpansion
for /f "usebackq delims==" %%A in (`dir /b *.pdf`) do (
set "prefix=%%~nA"
set "outname=!prefix!-%%03d-00.pdf"
"D:\PDFtk Server\bin\pdftk.exe" "!prefix!.pdf" burst output "D:\Temp\OUT\!outname!"
)
The whole command could be simplified to ONE line however to show where cuts can be made
try this
CD /D "D:\Temp"
for /f "usebackq delims==" %%A in (`dir /b IN\*.pdf`) do (
"D:\PDFtk Server\bin\pdftk.exe" "IN\%%~A" burst output "OUT\%%~nA-%%03d-00.pdf"
)
the need for usebackq delims== is because of later problems with characters such as filename (1).pdf
so we cannot shorten much more than
CD /D "D:\Temp" & for /f "usebackq delims==" %%A in (`dir /b IN\*.pdf`) do ("D:\PDFtk Server\bin\pdftk.exe" "IN\%%~A" burst output "OUT\%%~nA-%%03d-00.pdf")

Getting Parent Directory for each file from Dir Output

I have a directory with multipe levels of folders.
I am completely new to writing batch files and I am writing my first one.
Stuck for ages on trying to
find all files in the directory including sub-folder
get parent directory for each file
save as variable like %parent.filename%
I have been searching here:
https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb490909(v=technet.10)
And on Google but unfortunately I am stuck.
So now I managed to save the full path of each file as variable, but I want %Folder.FileName% to return the parent directory only, not the full path.
This is the code I have been testing in the command prompt.
For /F %A in ('Dir Linkedin /A-D /s /b /o') do SET Folder.%~nxA=%~pA
EDIT
I also saw this thread
And tried this code:
FOR /F %A in ('Dir Linkedin /A-D /s /b /o') do ECHO %~nxA %~pA >>Paths.txt
FOR /F "tokens=1,2" %A in (Paths.txt) do SET Parent.%A=%~nB
But %~nxB doesn't return any value... I expected it to get the last string of the path.
FOR /F %A in ('Dir Linkedin /A-D /s /b /o') do ECHO %~nxA %~pA. >>Paths.txt
FOR /F "tokens=1,2" %A in (Paths.txt) do SET Parent.%A=%~nB
Note the extra .
The path provided by the ~p modifier terminates in \ so adding . to this means "the directory name itself as though it was a filename"
As a one-line command (within a batch, decorated by standard palaver)
#ECHO OFF
SETLOCAL
FOR /F %%A in ('Dir test* /A-D /s /b /o') do FOR /F %%S in ("%%~pA.") do SET Parent.%%~nxA=%%~nS
set parent.
GOTO :EOF
I used the filemask test* to better suit my system.
I can't imagine you'd voluntarily perpetually re-type the command, so the format for use within a batch file is shown.
I would suggest you do this as a single nested For loop from the Command Prompt and with no output file:
For /F "Delims=" %A In ('Dir /B/S/A-D "Linkedin" 2^>NUL')Do #For %B In ("%~pA.")Do #Set "Folder.%~nxA=%~nxB"
From a batch-file, perhaps this would help you out:
#Echo Off
Rem Remove any existing Folder. variables
For /F "Tokens=1*Delims==" %%A In ('Set Folder. 2^>NUL')Do Set "%%A="
Rem Set the new variables
For /F "Delims=" %%A In ('Dir /B/S/A-D "Linkedin" 2^>NUL')Do For %%B In ("%%~pA.")Do Set "Folder.%%~nxA=%%~nxB"
Rem View any returned variables
Set Folder. 2>NUL&&Pause

How to return the newest file from each sub-directory in Windows command line using batch

I am using batch script for windows command line on windows 7. I am currently trying to get the newest file from each sub directory and print them to the screen. For example, if I have:
C:\Home\Hi\Folder1\a 01/05/2016
C:\Home\Hi\Folder1\b 01/10/2016
C:\Home\Hi\Folder2\x 03/05/2016
C:\Homeh\Hi\Folder2\y 03/1/2016
It would return: folders b and x.
I have written some script to attempt this, but it only returns the newest file in the last directory which in my example would be x. The script is:
FOR /R "C:\Users\Maxx\Desktop\tools" %%G in (.) DO (
Pushd %%G
FOR /F %%I IN ('DIR %%G /B /O:D') DO SET NEWEST=%%I
ECHO %NEWEST%
Popd )
Echo "back home"
If anyone knows how to get the newest file from each subdirectory that would be great.
Note: I have looked at various other examples such as: Generate a list of the newest file in each subdirectory in windows batch, which has been very helpful in building what I have now, but ultimately it did not work.
You need to apply delayed expansion as you are writing and reading variable NEWEST within the same block of code:
setlocal EnableDelayedExpansion
for /R "C:\Users\Maxx\Desktop\tools" %%G in (.) do (
pushd "%%~G"
for /F %%I in ('dir "%%~G" /B /O:D') do set "NEWEST=%%I"
echo !NEWEST!
)
popd
echo back home
endlocal
Alternatively, replace echo %NEWEST% by call echo %%NEWEST%%:
for /R "C:\Users\Maxx\Desktop\tools" %%G in (.) do (
pushd "%%~G"
for /F %%I in ('dir "%%~G" /B /O:D') do set "NEWEST=%%I"
call echo %%NEWEST%%
)
popd
echo back home
In addition, I improved quoting.
#ECHO OFF
SETLOCAL
FOR /R "C:\106x" %%G in (.) DO (
Pushd "%%G"
SET "first="
FOR /F "delims=" %%I IN ('DIR . /a-d /B /O:-D 2^>nul') DO IF NOT DEFINED first SET first=Y&ECHO %%~dpI
Popd
)
Echo "back home"
GOTO :EOF
For each directory, go to the directory, clear a flag first, scan the directory for files (not directories), basic format, reverse-date order, suppressing any "file not found" reports from empty directories.
On finding the first (ie youngest) file set the flag first and report the filename - or parts of the filename you require.
Once first has been set, the report mechanism will not be invoked as if defined works on the current value of the variable.
Please note that when specifying dates, it's advisable to state the format that you are using - or at least use a date where the day number is >12, which should be adequate. For instance, your date "01/10/2016" may be interpreted as Jan 10th or Oct 1st, depending on the respondent's locale. "13/10/2016" or "10/13/2016" would make the issue obvious.

List all files with 2 timestamps, size, but without path or dir

Windows, Command Prompt, need to generate a .txt file output containing of all files from a big and complex dir tree with one (1) line for each files as:
CreationDateYYYYMMDD-HHMMSS, LastModifiedYYYYMMDD-HHMMSS, filesize[no K commas], filename.ext
for example:
20100101-174503, 20120202-191536, 1589567, myfile.ext
The list should not contain lines of dir name entries, etc., only filenames, even if the same file is present in more than once. Time in 24 hours format.
dir /s/t:c/t:w/-c > filelist.txt
command does not exactly works this way.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=c:\program files"
FOR /f "delims=" %%a IN (
'dir /s /b /a-d "%sourcedir%\*" '
) DO (
FOR %%d IN (timewritten timecreated) DO SET "%%d="
FOR %%k IN (-d s h) DO (
IF NOT DEFINED timewritten FOR /f "tokens=1,2 delims= " %%d IN ('dir /tw %%~k "%%a" 2^>nul ^|find "%%~nxa"') DO SET "timewritten=%%d %%e"
IF NOT DEFINED timecreated FOR /f "tokens=1,2 delims= " %%d IN ('dir /tc %%~k "%%a" 2^>nul ^|find "%%~nxa"') DO SET "timecreated=%%d %%e"
)
ECHO !timecreated! !timewritten! %%~za %%~nxa
)
)
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
Interesting problem. This code processes it by
First, applying the standard directory-list for filenames on the tree from the relative root (%sourcedir%) to %%a
Using the full filename in %%a, set timewritten and timecreated from an ordinary dir list targeting the file in question.
It appeared that %%~ta didn't play nicely to extract the timestamp for hidden and system files, so I decided to build them from the ordinary dir listing with the appropriate t setting, specifically listing with /a-d, /as and /ah and filtering for the line which matched the filename, which seemed to extract the data appropriately.
I left the date/time in raw format. It should be an easy task to extract the various elements and construct the report in the format you want.
This question is a dupe of the SO post cmd dir /b/s plus date, but posting what worked for me:
#echo off
REM list files with timestamp
REM Filename first then timestamp
for /R %I in (*.*) do #echo %~dpnxI %~tI
#echo off
REM list files with timestamp
REM Timestamp first then name
for /R %I in (*.*) do #echo %~tI %~dpnxI
The above are the versions that you would directly paste into a command prompt.
If you want to use these in a batch file and log the output, you could do something like:
rem: Place the following in a batch file such as DirectoriesBareWithTS.cmd.
rem: As the first step in the batch file, net use to the directory or share you want the listing of
rem: Change to your target directory
Y:
for /R %%I in (*.mp4) do #echo %%~tI %%~dpnxI
Then you can pipe the output to a log file when you execute:
DirectoriesBareWithTS.cmd > C:\temp\GiantLongDirListing.log
You could then import that log into Excel.

batch file to search sub directories

I have a script i am trying to alter to search multiple sub directories within a given path, then copy the files found to location create folder with the copied files in folder file was ran from. At the moment script works but does to search sub directories, How do i alter my script below please.
#echo off
set LIST= C:\batch\list.txt
set FILESPATH="C:\Test"
for %%i in ("%LIST%") do set DEST=%%~ni
for /F "usebackq delims==" %%i in (%LIST%) do (call :COPY_FILES "%%i")
:COPY_FILES
xcopy /qv %FILESPATH%\%1 .\%DEST%\*
I have tried to alter this line
for /F "usebackq delims=" %%i in ('dir /s /b "%LIST%"') do (call :COPY_FILES "%%i"), but gets an error that the file list.txt could not be found.
Thanks
Test this: it will only print the xcopy lines to the console.
If it seems right then remove the echo from the xcopy line and run it for real.
If the commands are not right then provide a sample of the lines to see which part is wrong.
#echo off
set "LIST=C:\batch\list.txt"
set "FILESPATH=C:\Test"
for /F "usebackq delims=" %%i in ("%LIST%") do (
echo xcopy /s/h/k/f/c/q/v "%FILESPATH%\%%i" "%%~ni\"
)
pause

Resources