I have a simple task which I want to perform in cmd. I have a python file named Export_Excel.py stored in D:\Getting_Started_PyCharm\pyradiomics which I want to copy in many directories.
More specifically I have 30 directories which are inside a folder called NRRD and they are called:
1_patient, 2_patient, ..., 30_patient
Is there a way to do this with a for loop and the copy function in cmd??
Thanks a lot in advance
rem You should replace "" with NRRD's path in the file
set temppath=""
FOR /R %temppath% %%G in (.) DO (
Pushd %%G
copy D:\Getting_Started_PyCharm\pyradiomics\Export_Excel.py %%G
Popd )
echo Copying is done.
Example for PUSHD and POPD
C:\Program Files> PUSHD c:\utils
C:\utils> PUSHD c:\Windows
C:\Windows>
C:\Windows> POPD
C:\utils>
C:\utils> POPD
C:\Program Files>
This is the updated code:
Save this file in notepad as forcopy.bat and run it from the folder.
set temppath=C:\Users\tommaso\Desktop\Lavoro\Zurich_Fellowship\Myocardial_Infarction_DICOM_Images_19_04 _Copia\NRRD
cd %temppath%
for /r %temppath% %%G IN (.) DO (
Pushd %%G
copy "C:\Users\tommaso\Desktop\Excel_Export.py" %%G
Popd
)
del Excel_Export.py
cls
Echo Copying is done
pause
rem //Pause is to pause the execution and wait until the user presses a key.//
In the end, I found a python alternative which is even easier:
import os
from shutil import copy
path_to_file = "/.../.../Export_Excel.py"
path_destination_folder = "/.../.../NRRD/"
for sub_folders in os.listdir(path_destination_folder):
copy(path_to_file, os.path.join(path_destination_folder, sub_folders))
Related
I run the following in a cmd prompt in order to convert .webp files to .jpg, but have to go into each subfolder, one at a time, and open a new command prompt window, ad infinitum...
for %f in (*.webp) do "X:\12X\11Web\Games\webp (WEBPs)\libwebp-1.0.2-windows-x86\bin\dwebp.exe" -o "%~nf.jpg" "%f"
DEL "*.webp"
EXIT
I want to create a batch file that will cycle thru main folder as well as each subfolder tree, executing the above. I've gleaned from some other stackoverflow posts the following, but it's erroring out.
#echo off
FOR /d /r %%i IN (*) DO (
pushd "%%i"
COMMAND HERE????????
popd
)
Any help is appreciated.
Here you have two ways to do this. Either pushd to the directory where the file is found, run the command, test if the jpg file exists then delete the webp file:
Note, this is meant for a batch-file and not cmd.
#echo off
for /R %%f in (*.webp) do (
pushd "%%~dpf"
"X:\12X\11Web\Games\webp (WEBPs)\libwebp-1.0.2-windows-x86\bin\dwebp.exe" -o "%%~nf.jpg" "%%~nxf"
if exist "%%~nf.jpg" del /Q "%%~nxf"
popd
)
Or you can simply run it by converting without pushd by simply telling it the full path to file and output file:
#echo off
for /R %%f in (*.webp) do (
"X:\12X\11Web\Games\webp (WEBPs)\libwebp-1.0.2-windows-x86\bin\dwebp.exe" -o "%%~dpnf.jpg" "%%~f"
if exist "%%~dpnf.jpg" del /Q "%%~f"
)
I'm trying to write a batch file that will be executed by a filewatcher once a zip file is downloaded. After download the file is unzipped with the same name, then the zip file is deleted leaving only the file folder and a single PDF file inside. I just need the command to complete this action for the same file that is unzipped.
7z x -oC:\Users\"user"\Downloads\* C:\Users\"user"\Downloads\*.zip
del C:\Users\"user"\Downloads\*.zip
"command opening the file in the unzipped folder"
exit
#echo off
setlocal
:: Start in this dir.
cd /d "%userprofile%\Downloads" || exit /b 1
:: Dir where 7z extracts to.
set "extractdir=unzip_tmp"
:: Zips move here if files or folders exist in cd.
set "faildir=unzip_fail"
rem Continue only if zip files exist.
if not exist *.zip (
echo No zip files
exit /b 0
)
for %%A in (*.zip) do (
set "fail="
rem Unzip with 7z.
echo Unzip: "%%~nxA"
7z x -o"%extractdir%" "%%~nxA" >nul
if not errorlevel 1 (
pushd "%extractdir%" && (
rem Check if files or folders exist in parent dir.
for /f "delims=" %%B in ('dir /b') do (
if exist "..\%%~B" (
echo Exist: "..\%%~B"
set "fail=defined"
)
)
rem Open PDF file.
for /r %%B in (*.pdf) do (
echo Open: "%%~nxB"
start "" /wait "%%~fB"
)
rem Move files or folders to parent dir.
if not defined fail for /f "delims=" %%B in ('dir /b') do (
echo Move: "%%~B"
move "%%~B" .. >nul
)
popd
)
rem Cleanup.
if defined fail (
if not exist "%faildir%" (
echo Create: "%faildir%"
md "%faildir%"
)
echo Remove: "%extractdir%"
rd "%extractdir%" /s /q
echo Move: "%%~nxA"
move "%%~nxA" "%faildir%" >nul
) else (
echo Remove: "%%~nxA"
del "%%~nxA" >nul
)
)
)
rem Final cleanup.
if exist "%extractdir%" (
echo Remove: "%extractdir%"
rd "%extractdir%" /s /q
)
This uses a preset directory to extract the zip files to.
The variable named extractdir has the name of the folder to use.
A preset directory allows the ability to search for PDF files in
an isolated path.
The script is quite verbal so can see what is unzipped, moved etc.
Code operation:
Change directory to Downloads directory.
Exit if no zip files.
Search for zip file.
Extract from zip file to %extractdir%.
Check if extracted files and folders exist in parent directory.
If so, set fail to defined.
Search recursively for PDF files and open them.
if fail is not defined, move files and folders to parent directory.
Cleanup by deleting zip file or removing %extractdir%
and moving the zip file to %faildir% if fail is defined.
Finally remove %extractdir% if exist so it cannot foul up next run.
Use FOR, if you have multiple zip files, it will iterate over them all.
#echo off
for %%I in ("%UserProfile%\Downloads\*.zip") do (
"%ProgramFiles%\7-Zip\7z.exe" e -aoa "-o%%~dpI" -y -- "%%I"
if not errorlevel 1 (
del "%%I"
if exist "%%~dpnI.pdf" start "" "%%~dpnI.pdf"
)
)
This script says, here is the download path. For each PDF in this path, unzip and then delete the zip, open any PDF that was unzipped.
For those that want to try a fast one liner without dependencies this will depend on the file monitor calling the cmd line (as many can do)
you can add a command to go to a working download directory as required then for example if this page was in the zip as PDF run
forfiles /m *.zip /C "cmd /c md #fname && move #fname.zip #fname && cd /d #fname && tar -mxf #fname.zip && start \"\" #fname.pdf"
To delete the zip add && del #fname.zip onto the end of above before the last "
There should be little need for error checks as its conditional && so should not overwrite, the first abort should be
A subdirectory or file blah blah already exists.
I am currently using a batch file which uses a programm called CriPackTools to extract the contents of a CPK file.
#echo off
for /r %%i in (*.cpk) do "CriPakTools.exe" "%%i" ALL
as you can see here it uses any cpk i drag onto the batch file and extracts the contents to the same directory.
Now how can I change it so it uses my XXXX.cpk, creates a folder named XXXX (the same as the file) and extracts its contents inside the folder.
Thanks
Assuming that the CriPakTools.exe extracting files into a current directory:
#echo off
for /r %%i in (*.cpk) do (
md "%%~dpni"
pushd "%%~dpni"
"CriPakTools.exe" "%%i" ALL
popd
)
Since you are using for /R loop, you are looping through all subfolders. If a .cpk file is located in a folder different than the %cd% or the location of CriPakTools.exe, then the command will fail. I would suggest the following code:
#echo off
pushd "C:\path\with\cpks"
for /R "C:\path\with\cpks" %%A IN (*.cpk) do (
cd "%%~dpA"
mkdir "%%~nA"
cd %%~nA
"CriPakTools.exe" "%%~fA" ALL
)
cd "C:\path\with\cpks"
echo The operation has finished.
pause>nul
exit /b 0
This, may help you.
Here's what I have;
On a windows-based web server there are roughly 1,000 zip files, each with dozens of log files inside. I already have a script that goes through each archive and deletes all but one specific file type (in an attempt to save diskspace and delete things I don't need). Then, the script unzips each archive to their own folder. And I know how to code the reverse of that to zip them back when I'm done.
Here's what I need to figure out;
Once I run the previously mentioned script (we call it garbageman because it cleans out the garbage in the zip files) I need to go through the remaining 5 or 6 files in each of the newly created unzipped folders, and look for a specific string in each file. If I find the string, I delete everything that is not that string, and save it to a file called "export.txt" in that folder. Then, I move to the next unzipped file, and so on. Once completed, I need re-zip everything back together into their own archives
Here's what I have for code so far. Any help is extremely appreciated.
cd "C:\Program Files\7-Zip"
FOR %%c in (C:\Users\xxxxxx\Desktop\LogQueue\*.*) DO 7z d %%c "-x!xstore*" -r
FOR /R "C:\Users\xxxxxx\Desktop\LogQueue" %%I in ("*.zip") do (
"%ProgramFiles%\7-Zip\7z.exe" x -y -o"%%~dpnI" "%%~fI"
)
cd "C:\Users\xxxxxxx\Desktop\LogQueue"
FOR /R "C:\Users\xxxxxx\Desktop\LogQueue" %%I in ("*.*") do (
findstr "xxxxxxxx_eReceipt" %%~fI > %%~dpnI\export.txt
pause
)
for /d %%X in (*) do "c:\Program Files\7-Zip\7z.exe" a "%%X.zip" "%%X\"
This is an edit:
This script should do what you want.
Only Change sourcedir and mystring variables.
export.txt will be inside a file called Storage in the root directory of the batch.
:ScriptA
#ECHO ON
MKDIR "%CD%\Storage"
MKDIR "%USERPROFILE%\Desktop\Outx"
GOTO :ScriptB
:ScriptB
::REM ONLY CHANGE
SET "sourcedir=%USERPROFILE%\Desktop\Test"
FOR /R "%sourcedir%\" %%a in (*.txt) do copy "%%a" "%CD%\Storage"
:ScriptC
:ScriptC
#ECHO OFF
SETLOCAL
SET "VARA=%CD%\Storage"
SET "VARB=%USERPROFILE%\Desktop\Out"
::REM ONLY CHANGE
SET "mystring=PUT_STRING_HERE"
FOR %%a IN ("%VARA%\*.txt") DO FINDSTR "%mystring%" "%%a">nul&IF NOT ERRORLEVEL 1 FINDSTR "%mystring%" "%%a">"%VARB%\%%~nxa"
DEL /F "%CD%\Storage\*.txt"
GOTO :ScriptD
:ScriptD
#ECHO ON
COPY /B "%USERPROFILE%\Desktop\Outx\*.txt" "%CD%\Storage\export.txt"
RD /S /Q "%USERPROFILE%\Desktop\Outx"
goto :eof
I have source folder: c:\prefix\bin
I want to copy a set of specified files from source folder and its subfolders.
Lets say I want to copy:
bin
... gtsam.dll
... msvcr120.dll
... intel64\
... vc12\
... tbb.dll
To be more clearly for what I want to copy:
.\gtsam.dll .\msvcr120.dll .\intel64\vc12\tbb.dll
There are many files in the source directory and many subdirectories that I don't want to copy. And all the specified files I have to copy do not share a wild card. They are not all *.dll to copy to c:\dst
How I can do it with the most elegant way?
Using copy, or xcopy, or robocopy?
I suggest to create first a simple text file containing line by line the files to copy with relative path.
Example for FilesList.txt:
gtsam.dll
msvcr120.dll
intel64\vc12\tbb.dll
It is up to you in which directory to store this list file with the names of the files to copy.
The code below expects this file in directory C:\prefix\bin.
Then create a batch file with following code:
#echo off
pushd "C:\prefix\bin"
for /F "usebackq delims=" %%F in ("FilesList.txt") do (
%SystemRoot%\System32\xcopy.exe "%%~F" C:\dst\ /C /H /I /K /Q /R /Y >nul
)
popd
If target is specified with a backslash at end as done here and option /I is used, console application xcopy expects that C:\dst is a directory and even creates the entire directory structure to this directory automatically if not already existing.
Or you use this script with command copy and making sure the destination directory exists before copying the files.
#echo off
if not exist "C:\dst" (
md "C:\dst"
if errorlevel 1 (
echo.
echo Failed to create directory C:\dst
echo.
pause
goto :EOF
)
)
pushd "C:\prefix\bin"
for /F "usebackq delims=" %%F in ("FilesList.txt") do (
copy /B /Y "%%~F" C:\dst\ >nul
)
popd
Command md creates also entire directory tree on creating a directory with command extensions enabled as by default.
In both cases the directory C:\dst contains after batch file execution:
gtsam.dll
msvcr120.dll
tbb.dll
The main advantage of using a list file containing the names of the files to copy with relative path is easy updating in future without the need to change the batch code.
But let's say the files should be copied with duplicating the directory structure from source to destination directory resulting in directory C:\dst containing after batch file execution:
gtsam.dll
msvcr120.dll
intel64
vc12
tbb.dll
In this case the batch code with xcopy could be:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
pushd "C:\prefix\bin"
if "%CD:~-1%" == "\" ( set "BasePath=%CD%" ) else ( set "BasePath=%CD%\" )
for /F "usebackq delims=" %%F in ("FilesList.txt") do (
set "SourcePath=%%~dpF"
set "RelativePath=!SourcePath:%BasePath%=!"
%SystemRoot%\System32\xcopy.exe "%%~F" "C:\dst\!RelativePath!" /C /H /I /K /Q /R /Y >nul
)
popd
endlocal
And the batch code using copy could be:
#echo off
if not exist "C:\dst" (
md "C:\dst"
if errorlevel 1 (
echo.
echo Failed to create directory C:\dst
echo.
pause
goto :EOF
)
)
setlocal EnableDelayedExpansion
pushd "C:\prefix\bin"
if "%CD:~-1%" == "\" ( set "BasePath=%CD%" ) else ( set "BasePath=%CD%\" )
for /F "usebackq delims=" %%F in ("FilesList.txt") do (
set "SourcePath=%%~dpF"
set "RelativePath=!SourcePath:%BasePath%=!"
md "C:\dst\!RelativePath!" 2>nul
copy /B /Y "%%~F" "C:\dst\!RelativePath!" >nul
)
popd
endlocal
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
copy /?
echo /?
endlocal /?
if /?
for /?
goto /?
md /?
pause /?
popd /?
pushd /?
set /?
setlocal /?
xcopy /?
Not tested:
#echo off
set "basedir=c:\prefix\bin"
set "destination=c:\destination"
for /r "%basedir%" %%# in (*gtsam.dll *msvcr120.dll *tbb.dll) do (
copy "%%~f#" "%destination%"
)