Batch - loop misses commands after few passes - loops

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

Related

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.

check the existence of the folder and delete the temp files

I use batch file commands to delete the temp files in the system. The command works OK.
This code, works normally, but there is a flaw:
FOR /D %%p IN ("C:\Temp\*.*") DO rmdir "%%p" /s /q
cd c:\temp
del /F /s /q *.* >c:\DelTempLog.txt
rd /s /q %systemdrive%\$Recycle.bin >c:\DelTempLog.txt
FOR /D %%p IN ("C:\Windows\Installer\$PatchCache$\*.*") DO rmdir "%%p" /s /q
cd C:\Windows\Installer\$PatchCache$
del /F /s /q *.* >c:\DelTempLog.txt
FOR /D %%p IN ("C:\Windows\Temp*.*") DO rmdir "%%p" /s /q
cd C:\Windows\Temp
del /F /s /q *.* >c:\DelTempLog.txt
del /q /s %tmp% >c:\DelTempLog.txt
Today I faced an exception where c:\temp folder did not exist on the server.
It deleted half of the files under c:\windows\system32.
I want to add an IF command after changing the DIR before deleting anything.
Also, please advise me how to do logging activity in a better way.
At an elementary level if you specify the full path on the command line then it cannot delete files from anywhere else.
del /F /s /q "c:\temp\*.*?"
There is also no need to change the directory before issuing the command.
The ? suppresses a prompt that asks if you are sure that you want to delete all files.
How about, before your batch-as-it-stands, you try
md c:\temp 2>nul
if not exist c:\temp\. echo No c:\temp!&goto :eof
#echo off
setlocal enableextensions disabledelayedexpansion
set "logFile=c:\deltemplog.txt"
(for %%a in (
"c:\temp" "c:\windows\temp" "%temp%"
"%systemdrive%\$Recycle.bin" "C:\Windows\Installer\$PatchCache$"
) do if not exist "%%~a\" (
echo [ ERROR ]: "%%~a" does not exist
) else pushd "%%~a" && (
echo [ pushd ]: changed to "%%~a"
echo rmdir . /s /q
popd
) || (
echo [ ERROR ]: Failed to change to "%%~a"
)
) > "%logFile%"
For each folder in the list, change to it and if the command did not fail, remove the current folder (this will remove the content, not the folder, as it is the current one).
The rmdir commands are only echoed. If the output (in the log file) is correct, remove the echo command that prefixes rmdir
My preference is to use the %CD% built-in value, then try to move to the folder and see if it worked, as in:
set CURDIR=%CD%
pushd C:\Temp
if '%CD%'=='%CURDIR%' (
echo Failed to move to C:\Temp
) else (
[code to do your deletions]
)
popd
I prefer to push and pop a directory rather than a hard CD to it, simply because then you can always get back to where you were without having to know where that was.

Set files timestamp using cmd (.bat - recursive)

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
)

Command to delete files in UNC path

Hi I tried below command to delete files in UNC path
set folder="\\SERVERNAME\Publish"
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
But I got error saying:
UNC paths are not supported. Defaulting to Windows Directory
Somehow I need to delete files that are residing in Server's shared path using batch command. Any help appreciated.
edited 2015-09-16 - Original answer remains at the bottom
Code reformated to avoid removal of non desired folders if the mapping fails. Only if the pushd suceeds the removal is executed.
set "folder=\\SERVERNAME\Publish"
pushd "%folder%" && (
for /d %%i in (*) do rmdir "%%i" /s /q
popd
)
original answer:
set "folder=\\SERVERNAME\Publish"
pushd "%folder%"
for /d %%i in (*) do rmdir "%%i" /s /q
popd
pushd will create a drive mapping over the unc path and then change to it. Then, all the operations are over drive:\folders. At the end popd will remove the drive assignation.
This deletes all files with name like 'ms' and over a year.
#echo off
set "year=-365"
PushD "\\SERVERNAME\FolderName" && (
"forfiles.exe" /s /m "*_ms_*" /d %year% /c "cmd /c del #file"
) & PopD

Resources