Set files timestamp using cmd (.bat - recursive) - file

I have code:
#echo off
for /d %%a in (*.*) do (
pushd
echo %%~fa
cd %%~fa
cd
popd
copy /b "*.*" +,,
)
:end
pause
What I'm trying to do, is to touch all the files in the current folder (this .bat is already in this folder and doesn't matter) and subfolders with the current timestamp. Script is nearly done, but does not do what I exactly want.

for /d /r . %%f in (*) do (
pushd "%%~ff"
copy /b "*.*" +,,
popd
)

Related

loop through folder, copy and delete

I have a folder That has subfolders as below : I was able to loop and copy the folder and subfolders from test1 to test 3. But I want to extend the functionality by removing the each folder after copying
but am not able to instead is removing all the test 1 to test 3 folder.The test folder is just an example, the folders can be any name.
"C:\BACKUP_FDM_FILES\localbackup\test1"
"C:\BACKUP_FDM_FILES\localbackup\test2"
"C:\BACKUP_FDM_FILES\localbackup\test3"
pushd "C:\BACKUP_FDM_FILES"
for /f "tokens=*" %%G in ('dir /b /s /a:d ') do (
echo Found %%G
XCOPY %%G "C:\FDM\Upload" /e /s /y
if errorlevel 0 (
echo Copy succesffully copied reason Files were copied without error and you can delete the content folder
rd "%%G\*" /s /q
mkdir "%%G"
)
)
popD
pause
If you resort to XCOPY, why not use the "successor" ROBOCOPY without any loop, deleting and re-creating the folder? Like in
robocopy "C:\BACKUP_FDM_FILES\localbackup\" "C:\FDM\Upload\localbackup" /s /e /COPYALL /dcopy:T /move
This will even keep the timestamp on the moved folder, and all attributes on moved files. Added benefit: you can use all the other helpful options of this command at no extra cost.
"C:\BACKUP_FDM_FILES\localbackup\test1"
"C:\BACKUP_FDM_FILES\localbackup\test2"
"C:\BACKUP_FDM_FILES\localbackup\test3"
pushd "C:\BACKUP_FDM_FILES"
for /f "tokens=*" %%G in ('dir /b /s /a:d ') do (
echo Found %%G
XCOPY %%G "C:\FDM\Upload" /e /s /y
if errorlevel 0 (
echo Successfully copied now you can delete folder
popd
rd "%%G\*" /s /q
mkdir "%%G"
)
pause
You are using pushd to go to backup folder so you need to use popd to go back to parent folder.
SS64 - POPD

Compress separately files within subfolders

Hi all and thanks for the answers,
Firstly, I tried to find the answer to my problem but I did not find anything.
I have a tree of folders and sub-folders and I want to use 7zip to compress the files within those folders separately.
I have got this piece of code from this very website, it does what I want to get but it places the compressed files on the main folder:
set extension=.*
for /R %%a in (*%extension%) do "%sevenzip%" a -mx "%%~na.zip" "%%a"
I wonder if I can get a zip file of every file and have it in the sub-folder containing the source file. Or doing the process above and place every zip file inside the appropriate sub-folder.
I tried with a double 'For /d' but I was unable to get it:
cd /d %~dp0
rem 7z.exe path
set sevenzip=
if "%sevenzip%"=="" if exist "%ProgramFiles(x86)%\7-zip\7z.exe" set
sevenzip=%ProgramFiles(x86)%\7-zip\7z.exe
if "%sevenzip%"=="" if exist "%ProgramFiles%\7-zip\7z.exe" set
sevenzip=%ProgramFiles%\7-zip\7z.exe
if "%sevenzip%"=="" echo 7-zip not found&pause&exit
for /D %%O in (*) do (
for /R %%I in ("%%O\*") do (
"%sevenzip%" a -mx "%%~na.zip" "%%a"
:: rd /s /q "%%I" **Because I do not want to delete anything by now.
)
)
Again, thank you.
Alex.
If you have somewhat complex folder structure then you probably better use plain list from dir:
dir /a:-d /s /b /o
Just use its output in for:
for /f %%f in ('dir /a:-d /s /b /o') do (
echo %%f <-- %%f is a full path to a file, do something with it
)
Btw, 7zip has useful option -sdel to remove the source file when archive has been created successfully.
This is the final code:
#echo off
cd /d %~dp0
rem 7z.exe path
set sevenzip=
if "%sevenzip%"=="" if exist "%ProgramFiles(x86)%\7-zip\7z.exe" set sevenzip=%ProgramFiles(x86)%\7-zip\7z.exe
if "%sevenzip%"=="" if exist "%ProgramFiles%\7-zip\7z.exe" set sevenzip=%ProgramFiles%\7-zip\7z.exe
if "%sevenzip%"=="" echo 7-zip not found&pause&exit
#echo searching...
for /R %%I in (*) do (
"%sevenzip%" a -mx -mmt4 "%%I.7z" -r -x!*.bat "%%I"
)
del "Compressing_files_7zip.bat.7z"*
del *.7z.7z
del *.zip.7z
::::::::::::::::::::::::::For setting up shutdown 60' after the end of the process.Remove colons in the line below.
::shutdown.exe /s /t 3600
pause
Thanks all for the support, especially to Frost.
For a simpler version without using 7zip:
for /f %%f in ('dir /a:-d /s /b /o *.mdb') do (
zip -r -p "%%f.zip" "%%f"
)

Open multiple folders with batch script

I have on my computer an partition named H:
On that partition i have diffent folders with each a folder called "bin"
Example:
H:MyFolder\Bin
H:AnotherFolder\Bin
I know that I can delete bin with the follow command:
set folder="H:\Bin"
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
Is it possible to empty all the bin folders from all folders in partition H: with a command?
This will remove the content of all the bin folders under H:\ while keeping the folders.
for /r "H:\" /d %%a in (bin) do #if exist "%%~fa\" ( pushd "%%~fa" && ( echo "%%~fa" & echo rmdir . /s /q & popd ))
There is an echo command that precedes the rmdir. This is included for testing and must be removed to perform the content removal.

How to zip all sub folders inside folder with a specific name

I am looking to find all folders with the name "Logfile" inside slightly different folder structures. For example how would I find the Logfile folder inside C:\ECU\ECU1\Logfile, C:\ECU\ECU2\Logfile, C:\ECU\ECU3\Logfile and C:\ECU\ECU4\Logfile? I then want to zip the .txt contents of this folder in each case. I currently have a batch file running which allows me to zip the contents of a folder which has the same folder structure each time but need to combine the above all together. Any help would be great...
Thanks.
#echo off
pushd "C:\ECU\ECU2" || goto :eof
REM zip all files in the backup directory
FOR %%A IN (*.TXT*, *.cpi*) DO "C:\Program Files\WinRAR\WinRAR.exe" a -r "%%~nA.zip" "%%A"
FOR %%A IN (*.TXT,*.cpi) DO DEL "C:\ECU\ECU2.cpi*" "%%A"
popd
Try this as a test first to see if it prints the correct folders:
#echo off
for /d /r "c:\ecu" %%a in (target*) do (
if /i "%%~nxa"=="target" (
echo "%%a"
)
)
pause
and if it's ok then this should work - test it with dummy files, but your DEL command is odd in the first term. I replaced it with what might work.
#echo off
for /d /r "c:\ecu" %%a in (target*) do (
if /i "%%~nxa"=="target" (
pushd "%%a"
REM zip all files in the backup directory
FOR %%A IN (*.TXT* *.cpi*) DO "C:\Program Files\WinRAR\WinRAR.exe" a -r "%%~nA.zip" "%%A"
FOR %%A IN (*.TXT *.cpi) DO DEL "%%A"
popd
)
)

Batch - loop misses commands after few passes

I tried to write simple program. It should find .pak files (zip in this case but with changed etension), extract them and pack them using arc.
#echo off
for /r %%i in (*.pak) do ren %%~i %%~ni.arc
for /r %%i in (*.arc) do (
mkdir %%~ni
cd %%~ni
..\arc.exe x -o+ "%%~i" //extract archive at current location//
del "%%~i"
..\arc.exe a -m9 -r "%%~i" *.* //pack files and folders in current folder and create
archive at specific location//
cd..
RD /s /q %%~ni
)
pause
Arc command works i'm sure about that. Everything works perfect untill few passes later. It seems that it suddenly stop doing cd.. command because it starts creating folder then folder inside then again and again.
Locations of files:
...\arc.exe
...\program.bat
...\file1.pak
...\folder1\file2.pak
...\folder2\file3.pak
etc for pack
I tried also
#echo off
setlocal enableextensions enabledelayedexpansion
for /r %%i in (*.pak) do ren %%~i %%~ni.arc
for /r %%i in (*.arc) do (
mkdir %%~ni
cd %%~ni
..\arc.exe x -o+ "%%~i"
del "%%~i"
..\arc.exe a -m9 -r "%%~i" *.*
cd..
RD /s /q %%~ni
)
pause
The same result
You definitely do not want to enable delayed expansion since it will corrupt file names if they happen to contain ! character.
I believe your problem is simply you haven't quoted your path/file names. Spaces and special characters in path/name will cause problems unless they are quoted.
This is completely untested, but I think it may fix you problem:
#echo off
for /r %%i in (*.pak) do ren "%%i" "%%~ni.arc"
for /r %%i in (*.arc) do (
mkdir "%%~ni"
cd "%%~ni"
..\arc.exe x -o+ "%%~i" //extract archive at current location//
del "%%i"
..\arc.exe a -m9 -r "%%i" *.* //pack files and folders in current folder and create archive at specific location//
cd..
rd /s /q "%%~ni"
)
pause

Resources