I am a php developer and I know nothing about windows batch files.
Maybe somebody will help me...
I have a directory structure like here:
directory1/1/
directory1/2/
directory1/5/
directory2/1/
directory2/2/
directory2/5/
etc...
How can I write a bat file that will do the following:
-move all files from all sub-directories '1' into a different directory
I had tried this
pushd %CD%\in\
for /r %%a in (*.*) do (
echo COPY "%%a" "%CD%\out\%%~nxa"
)
popd
but this code takes all the files from "in" folder and copies them to "out" folder. How can I determine subdirectories here?
You can do it with this batch command:
#echo off
::Set Directory
set Dir=%CD%\in
::Set Destination
set Des=%CD%\out
::Set Sub Directory you want to move
set SubDir=1
if exist "%Dir%\*" (
if not exist "%Des%\*" mkdir "%Des%"
for /D %%a in ("%Dir%\*.*") do (
for /D %%b in ("%%a\*.*") do (
if "%%~nxb" EQU "%SubDir%" ROBOCOPY "%%b" "%Des%\%%~nxa\%%~nxb" /E /IS /MOVE>Nul
)
)
)
Related
I have many .zip files and folders some of them have identical names.
I will check zip file name and folder name if it is identical.
I will copy zip file into the folder and delete the zip file.
Is it possible to Automate the process?
I have found following code but couldn't edit it:
#echo off
setlocal EnableDelayedExpansion
pushd "C:\New folder"
FOR %%G IN (*.zip DO (
FOR /F "tokens=1 delims= " %%a IN ("%%G") do (
set "outFolder=%%a Random Center"
for /D %%i in (*.*) do (
for /F "tokens=1 delims= " %%b IN ("%%i") do (
if "%%a"=="%%b" set "outFolder=%%i"
)
)
if not exist "!outfolder!" md "!outfolder!"
move "%%G" "!outfolder!"
)
)
popd
pause
It creates Random Center folder and copy all the zip files in it which I don't want.
#echo off
setlocal
pushd "C:\New folder" || exit /b 1
for %%A in (*.zip) do if exist "%%~nA" (
pushd "%%~nA" && (
move /y "..\%%~nxA"
popd
)
)
popd
pause
Using move as see no need to
copy and del the zip file.
Argument /y will automatically
overwrite an existing file without
prompt.
Initial pushd changes the current directory
else exits with errorlevel 1.
The for loop iterates through each zip filename.
It checks if the name of the filename, without extension,
does exist, which is expected to be a folder.
If does exist, pushd into the directory, and then
move the zip file from the parent directory into
the current directory. popd will restore to the
previous directory.
Does final popd to restore to initial current directory.
I need a batch file to zip files inside a folder with a specific file type. The file type is (.asc).
Could anyone help me correct the code I have below?
Thanks.
for /r "C:\Test_Results\Report_%Date:~-10,2%_%Date:~-7,2%_%Date:~-4,4%" %%G in (.asc*)do (
if /i "%%~nxa"==".asc" (
pushd "%%a"
REM zip all files in the backup directory
FOR %%A IN (*.asc* *.cpi*) DO "C:\Program Files\WinRAR\WinRAR.exe" a -r "%%~nA.rar" "%%A"
FOR %%A IN (*.asc *.cpi) DO DEL "%%A"
popd
)
)
The code below zips files inside any folder called Logfile. This works well but I now need to do something a little different (as mentioned in the first post).
#J.Baoby, I have changed the .asc* to *.asc, but I'm still having issues with zipping the files. I'm actually not sure what the IF statement after the FOR loop is doing. Is it needed here?
#echo 1. This step zips all files inside any folder with the name Logfile under C:\TestReports\Report_today's date.
for /d /r "C:\TestReports\Report_%Date:~-10,2%_%Date:~-7,2%_%Date:~-4,4%" %%a in (Logfile*) do (
if /i "%%~nxa"=="Logfile" (
pushd "%%a"
REM zip all files in the backup directory
FOR %%A IN (*.blf* *.cpi*) DO "C:\Program Files\WinRAR\WinRAR.exe" a -r "%%~nA.zip" "%%A"
FOR %%A IN (*.blf *.cpi) DO DEL "%%A"
popd
)
)
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
)
)
I am looking for a simple batch or vbs script I can edit to complete the following tasks.
Search a folder (including sub dirs) for *.rar files.
Extract the *.rar found to a specific drive i.e E:/ or F:/ (I can change the file for this)
The twist, is the script must rename the extracted file to the directory name.
i.e
C:\Documents\Shop1_A\file.rar
Inside file.rar there is a file.pdf
I require the script to extracted the file.rar to a drive and rename the extracted file to E:\Shop1_A.pdf
There will only ever be 1 file in the archive (no duplicate or overwrite errors)
set "sourceDir=c:\someware"
set "targetDir=f:\"
set "unrar=c:\program files\WinRar\unrar.exe"
for /r "%sourceDir%" %%f in (*.rar) do for /d %%d in ("%~dpf\.") do (
"%unrar%" p -inul "%%~f" > "%targetDir%\%%~nd.pdf"
)
do you only have pdf files? If not, try this:
#ECHO OFF &SETLOCAL
set "SourceFolder=%userprofile%"
set "DestinationFolder=%temp%"
for /d /r "%SourceFolder%" %%a in (*) do for %%b in ("%%~fa\*.rar") do for /f "delims=" %%c in ('rar lb "%%~Fb"') do (
rar e -idq "%%~fb" "%%~c" "%DestinationFolder%"
ren "%DestinationFolder%\%%~c" "%%~na%%~Xc"
)
If you don't own rar, you can also work with the free unrar.
I've created a batch file which unzips all files
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
REM
REM Remove the double quotes from the front and end of the root path
REM
SET ROOT=%1
SET ROOT=%ROOT:~1%
SET ROOT=%ROOT:~0,-1%
ECHO %ROOT%
REM Searching directory structure from root for subfolders and zipfiles,
REM then extracting the zipfiles into a subfolder of the same name as the zipfile.
FOR /F "delims==" %%d IN ('dir /ogne /ad /b /s "%ROOT%"') DO (
ECHO Extracting : "%%d"
FOR /F "delims==" %%f IN ('dir /b "%%d\*.zip"') DO (
REM Getting filename without extension.
SET subfolder=%%~nf
ECHO mkdir "%%d\!subfolder!"
mkdir "%%d\!subfolder!"
REM Extracting zipfile content to the newly created folder.
ECHO 7z x "%%d\%%f" -o "%%d\!subfolder!"
"C:\Program Files\7-Zip\7z.exe" x "%%d\%%f" -o"%%d\!subfolder!"
)
)
ENDLOCAL
Now I want to delete the files which has been created.
Can anyone help in deleting the unzipped files alone and not the zipped files and also care should be taken that it should not delete the root and the zip file.
Any other solution is also appreciated.
I want the zip files only within the folders. All other files can be deleted without user saying Y/N.
Try this and remove the echos if the output is OK:
for /r "%root%" %%i in (*.zip) do (
echo del /f /q "%%~dpni\*.*"
echo rd /q "%%~dpni"
)