what im looking for is a .bat file code to zip files individually in all subfolders in the current folder and then delete the of files after, to exclude already zipped/compressed files, what i dont want is folders to be zipped and i want the files to keep there name when zipped
i have a bunch of folders/files and the only code i found
#ECHO OFF
FOR %%i IN (*.*) DO (
ECHO "%%i" | FIND /I "batch zip files.bat" 1>NUL) || (
"c:\Program Files\7-Zip\7z.exe" a -tzip "%%~ni.zip" "%%i"
if %ERRORLEVEL% == 0 del "%%i"
)
)
zips all files in the current directory and doesnt touch subfolders
i'd appreciate it if anyone can do this for me as i can save a ton of space with all files zipped
The first issue you have with your provided code is that your For loop is only parsing files in the current directory, there is no recursion into subdirectories. To parse files within the subdirectories, I'd advise that you use a For /F loop, with the Dir command using its /B and /S options. I would also advise that you include the attribute option, /A, which will include every item, then omit those which you're not interested in. For instance, it's unlikely that you want to zip the directories, hidden files, reparse points, or system files. You can do that by excluding those attributes, /A:-D-H-L-S. To learn more about the For command, and the Dir command, open a Command Prompt window, type for /?, and press the ENTER key. You can then do the same for the Dir command, i.e for /?. As you have not defined a working directory at the start of your script, it will run against every file and directory in whatever is current at the time you run it. Because your code has a line excluding a file named batch zip files.bat, I'm going to assume that is the name of your running script, and that your intention is to therefore run the script against everything in the tree rooted from the same location as the batch file itself. To ensure that is always the case, for safety, I've defined that directory as the current directory from the outset, using the CD command, CD /D "%~dp0". %0 is a special batch file argument reference to itself, to learn more about this please take a look at the output from both call /?. You can also learn about the CD command entering cd /?, in a Command Prompt window too. To also omit your batch file, as you don't want it to be zipped and deleted, I've piped the results from the Dir command through FindStr, printing only items which do not exactly match the case insensitive literal string %~f0 (expanding to the full path of the batch file itself). Additionally, I've piped those results through another findstr.exe command to omit any files already carrying a .zip extension, as there's no point in zipping files which already zip files. (Please note however, that for more robust code, you should really check that those are zip archives and not just files carrying a misleading extension). The results from those commands are then passed one by one to the Do portion which includes your 7z.exe command. I've assumed at this stage, that your intention was to save the zipped archives to the same location as the originating files. To do that I've used variable expansion on %%G to stipulate its directory, path, and name, %%~dpnG, (see the usage information under for /? to recap). Upon successful completion of the archiving process, the original file will be deleted, to do that I appended the -sdel option to your original command string. Please be aware that you may want to include additional options, should you wish to update existing zip files etc. (use "%ProgramFiles%\7-Zip\7z.exe" -? in a Command Prompt window to see them). As I've not mentioned it previously, at the beginning of the script, I made sure that extensions were enabled. Whilst it is the default option, it's safer to be sure, as variable expansion and the commands CD, and For can be affected, if they're not.
Here's the code as explained above:
#Echo Off
SetLocal EnableExtensions
CD /D "%~dp0"
For /F "EOL=? Delims=" %%G In ('Dir "*" /A:-D-H-L-S /B /S 2^> NUL ^|
%SystemRoot%\System32\findstr.exe /I /L /V /X "%~f0" ^|
%SystemRoot%\System32\findstr.exe /E /I /L /V ".zip"'
) Do "%ProgramFiles%\7-Zip\7z.exe" a -tzip "%%~dpnG.zip" "%%G" -sdel
Looking at your question, which has changed from what you'd asked initially, you appear to not be interested in the files of the batch file directory any more, "zip files individually in all subfolders in the current folder". For that reason, I've provided the following alternative, methodology.
The difference is that I first of all use a For loop to include only directories in the current working location, /A:D-H-L-S, before running the same method used in my previous example, but with one difference. As we're now no longer zipping files in the current working directory, we can remove the findstr.exe command filtering out the running batch file:
#Echo Off
SetLocal EnableExtensions
CD /D "%~dp0"
For /F "EOL=? Delims=" %%G In ('Dir "*" /A:D-H-L-S /B 2^> NUL'
) Do For /F "EOL=? Delims=" %%H In ('Dir "%%G" /A:-D-H-L-S /B /S 2^> NUL ^|
%SystemRoot%\System32\findstr.exe /E /I /L /V ".zip"'
) Do "%ProgramFiles%\7-Zip\7z.exe" a -tzip "%%~dpnH.zip" "%%H" -sdel
Please be aware, that my answers above are to essentially correct your code attempt, and not a personal recommendation for speed, or in performing the task laid out in your question. Additionally, I have no idea what will happen if any of those files are in use/locked, and have made no attempt at checking for such scenarios.
when it comes to creating a batch-file I would sadly call myself a newbie and therefore it's kind of difficult for me to achieve what I want on my own:
so here is how my codes look:
#ECHO OFF
for /r "C:\source" %%f in (*) do copy /y "%%f" C:\ReadyToExport
del C:\ReadyToExport\*.pdf*
for /f "delims=" %%i in ('xcopy "C:\ReadyToExport" C:\import /EXCLUDE:LogFile.log /S /E /D') do (
echo %%i >> C:\LogFile.log)
PAUSE
and here is what the code exactly does
1- for /r "C:\source" %%f in (*) do copy /y "%%f" C:\ReadyToExport
this line copies all files from source to another target folder the good thing is that this command copies all the files inside other subfolders from source and paste them into the folder C:\ReadyToExport without any subfolders.
2- del C:\ReadyToExport\*.pdf*
this command filters all the .pdf files because they are unnecessary
3-
for /f "delims=" %%i in ('xcopy "C:\ReadyToExport" C:\import /EXCLUDE:LogFile.log /S /E /D') do (
echo %%i >> C:\LogFile.log)
this command basically copies all files from "C:\ReadyToExport" into C:\import and writes the name of the recorded file in LogFile.log then the copied files will be excluded when the script runs again because I don't want to copy the files again if I already copied them before
and here is what I want to achieve:
I want to copy only modified files from the folder "C:\ReadyToExport" to the target C:\import but keep in mind that
the files in the target folder "C:\import" will be deleted but since the names of the files that have already been copied are registered in LogFile.log the files will be excluded and not copied again.
in another word: files in target file do not exist anymore but their names are written in LogFile
so is there any way to copy only modified ones? even though they don't exist in the target folder anymore? but their names are in LogFile.txt? can the script somehow write the last modified date in LogFile.txt near the file name? and then compare the date from the source? so it copies only files that have been changed and ignore all files that didn't?
p.s: using Xcopy, robocopy, etc is not a problem
any answer will be appreciated.
thnx
your method does, in fact, works so thank you so much not the way the I want with exclude:logfile.log
but this definitely worked:
robocopy C:\ReadyToExport\ C:\import /M /E
since it copies only the files that hast the attribute archivable checked in the settings.
I'm trying to make a simple batch file to "sync" everything between a file on my onedrive, and a file on a network drive.
I want to be able to run the batch file, and anything new on the onedrive will be copied to the network drive, but not the other way around.
Here is my batch file:
#echo off
echo : XCOPY started
xcopy "D:\OneDrive - Radford University\Classwork 2017-2018\Spring 2018" "H:\Spring 2018" /E /D /C /Y /EXCLUDE:C:\exclude.txt
echo : complete
when run, this file seems to copy anywhere between 80-83 files. It is different each time I run it, despite me making no changes to either directory.
My understanding is that the /D flag for xcopy will ensure that it only copies files that are new, or have a newer modified date. But in this case, it seems to copy everything that isn't in my exclude.txt, regardless of date modified.
I think you have the two options:
for /f %%G in (c:\exclude.txt) do set "_excludes=%_excludes% %%~G"
robocopy "D:\OneDrive - Radford University\Classwork 2017-2018\Spring 2018" "H:\Spring 2018" /MT /E /MIR /XF %_excludes%
or
robocopy "D:\OneDrive - Radford University\Classwork 2017-2018\Spring 2018" "H:\Spring 2018" /MT /E /MIR
for /f %%G in (c:\exclude.txt) do del "%%~G"
You test with the /L option to simply log what it would have done.
I am trying to copy all files from several folders to one folder in the same directory.
I have create a Batch file, which contains
MD PATCHCON
for /R %cd% %%f in (*.*) do copy %%f %cd%\PATCHCON
pause
If I put this on the desktop, it runs successfully; if I run the same code in the dir it's not working.
This code also copies my batch file in consolidate folder<patchcon> so I also want to add a code line that does not copy my batch file.
You have a couple of issues which you may not have realised.
Firstly it will fail in folders or files that have a space or & in the name or path.
The other issue is that it will try to copy some files in the PATCHCON folder twice.
This should solve those problems and remove the batch file itself from the folder.
#echo off
MD "..\PATCHCON"
for /R "%cd%" %%f in (*) do copy "%%f" "..\PATCHCON" >nul
del "..\PATCHCON" "%~nx0"
move "..\PATCHCON" . >nul
echo done.
pause
I am trying to write a batch to copy all files (those in subfolders too) from directory1 to directory2 and then moving any extra files (or directories if possible) found in directory2 to directory3.
In other words, I am trying to mirror directory2 with directory1 but instead of the purge action, moving the extra files to directory3...
So far, I've been able to get those extra files in a text file... Now, I don't know how to proceed...
ROBOCOPY directory1 directory2 /E /COPYALL /R:0 /XO /X /tee /njh /njs /np /ns /nc /ndl > Extrafiles.txt
Further, the Extrafiles.txt does contain a lot of extra spaces at start of each file path that means a little string manipulation before moving the file. And which command to use to move the files by automatically creating the target directory if it does not exist?
Sample output of Extrafiles.txt
directory2\File1.txt
directory2\subdirectory2\deeper\File2.txt
Any help will be appreciated. Thank You.
Making it a little more clear:
Suppose directory1 contains:
\subdir1
file4.txt
file5.txt
\deeper
file6.txt
file7.doc
file1.txt
file2.txt
and directory2 already contains:
\subdir1
file4.txt
file007.ppt
\deeper
file6.txt
file7.doc
\deepest
file155.txt
file1.txt
Then I want these files to be copied:
directory1\file2.txt to directory2\file2.txt
directory1\subdir1\file5.txt to directory2\subdir1\file5.txt
And then these files to be moved:
directory2\subdir1\file007.ppt to directory3\subdir1\file007.ppt
directory2\subdir1\deeper\deepest\file155.txt to directory3\subdir1\deeper\deepest\file155.txt
Hope I made it clear enough :)
You can use for to iterate through the contents of the file generated by robocopy (see for /?).
Then you can use delayed expansion and variable substitution to generate the target path (see set /?), and finally call a subroutine that will do mkdir if needed followed by move (see call /?).
A draft solution would be:
#echo off
setlocal enabledelayedexpansion
set srcdir=directory2
set tgtdir=directory3
for /f "usebackq tokens=*" %%A in ("Extrafiles.txt") do (
set srcpath=%%A
set tgtpath=!srcpath:%srcdir%=%tgtdir%!
call :mkdirmove !srcpath! !tgtpath!
)
goto :EOF
:mkdirmove
if not exist %~dp2nul mkdir %~dp2
move %1 %2
goto :EOF