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"
)
Related
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.
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))
I am a CMD newbie and have a question with a batch script I am working on.
I have a parent directory with 30 sub-directories containing .pdf files, and I need a filelist.txt for each sub-directory, and have each filelist.txt save as the file name of the sub-directory it belongs too. This has been completed with the script below:
#echo off
cd /d "C:\Desktop\parentDir"
for /d %%a in (*) do (
DIR /B /ON /A-D "%%a" > %%a.txt.
move %%a.txt "%%a" >nul
)
My question is how, can I remove file extensions in the output of each filelist.txt. For ex. when I run the script now, the output .txt file shows 1111.pdf
1112.pdf
I need the ".pdf" removed
I know with a "for" command you can "do" echo %%~na to remove file extensions, but I have no clue how/where to factor this into the current script.
Any help is appreciated!
You are using DIR command to list all files. It does not have a switch to hide extension.
You can replace the DIR command with a FOR loop, like you pointed out.
cd /d "C:\Desktop\parentDir"
for /d %%a in (*) do (
for %%f in ("%%a\*") do #echo %%~nf >> %%a.txt.
move %%a.txt "%%a" >nul
)
I am coding a batch file that will get the directory of a specific file(curl_for_64bit.exe). I tried using the find command but it does not work. It basically gets the directory of the file, changes to that directory so that it can be copied.
you can traverse the directory tree with a FOR command checking for the existence of the required file until it's found
for /r /d %%a in (*) do (
if exist %%a\curl_for_64bit.exe (
pushd %%a
goto :eof
)
)
you can use command like #Ken White say, then use other code to get the file's directory.
here is my code
rem you should go to the specific root directory(like c d e etc.)
cd /d c:\
dir /s /a /b curl_for_64bit.exe >tmp.txt
set /P file_path=<tmp.txt
del tmp.txt
cd /d %file_path%\..
And if you just want to copy those files, why you want to go to the directory of file?
The following script searches the file curl_for_64bit.exe in the directory tree rooted at C:\ROOT\ and changes to the parent directory where the found file is actually located:
for /F "delims=" %%F in ('dir /B /S /A:-D "C:\ROOT\curl_for_64bit.exe"') do (
cd /D "%%~dpF"
)
Or this command line to be typed directly into cmd:
for /F "delims=" %F in ('dir /B /S /A:-D "C:\ROOT\curl_for_64bit.exe"') do #cd /D "%~dpF"
To learn what the ~dp modifier of the for variable %%F means and how it works, open a new command prompt window, type for /? and read the help text (see the last section in particular).
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