This question already has an answer here:
Automating task for unzing a gz compressed file using windows batch script [closed]
(1 answer)
Closed 2 years ago.
I have two issues with this code I have attached below,
it doesn't delete the original file after unzip, I want the script to delete the original file from source folder
the converted file is not saved in target folder as set but it creates a tree of the directory in the same folder of the script and saved output there.
Please help me solve this issue
Sample code I have tried
#echo off
set "source=%userprofile%\Desktop\basanta\Automation\a"
set "target=%userprofile%\Desktop\basanta\Automation\b"
FOR %%A IN ("%source%\*.gz") DO (
"%programfiles%\7-zip\7z.exe" x "%%~A" -o"%target%\%%~pA"
del "%%~A" /Y
)
Please help me to write the script as described above
try: del file /f /q
try to enclose -o parameter with a quotes: "-o%target%"
Related
This question already has answers here:
Touch file to update the date/time of file? (Modified Date) (WindowsXP BatchScript)
(3 answers)
Closed 2 years ago.
I want to develop batch having step that it will update timestamps of all files within some directory.
Below command gives list of all files in directories.
Can we redirect output of this command to some other windows equivalent touch command, so as to update the timestamps for all files?
cmd>dir /B
Log_adminUser_1.log
Log_adminUser_2.log
Log_adminUser_3.log
Log_adminUser_4.log
Log_adminUser_5.log
Log_adminUser_6.log
As a trick for touching all files in a directory, you could use this:
#ECHO OFF
FOR /F "delims=" %%G IN ('DIR /B') DO (
COPY /B "%%G"+,, "%%G" > NUL
)
The COPY /B construct is documented in TOUCH on SS64, which also explains some caveats with it.
This question already has answers here:
Copy Contents From Folder Using Wildcard On The Directory
(1 answer)
xcopy wildcard source folder name to destination
(2 answers)
Using a batch file to copy files with a wildcard in the directory path?
(2 answers)
Closed 3 years ago.
I need to copy set of files from source to destination, below is the command I am using and its working fine. but folder "29749659" is dynamic and its name always changes. Under "unzipped" only one folder will exist.
xcopy /y C:\Nageswar\unzipped\29749659\files\products\Essbase\EssbaseClient\api\include\* C:\Jenkins\jobs\Planning\branches\develop\workspace\planning\Jni\include
Is there any way to write a command
for /d %%a in (C:\Nageswar\unzipped\*) do xcopy /y "%%a\files\products\Essbase\EssbaseClient\api\include\*" C:\Jenkins\jobs\Planning\branches\develop\workspace\planning\Jni\include
should accomplish this - the for /d scans the target directoryname for directorynames and applies each found in turn to %%a
This question already has answers here:
What is the difference between % and %% in a cmd file?
(2 answers)
Closed 3 years ago.
I receive files via FTP that are placed in a folder called Landing. These files arrive in a folder that is randomly named, for example 000174, and the files inside are named Activity.txt.174 for example.
I have created a small script that allows me to reduce the file name to Activity.txt which works perfectly fine when executed within a command shell but when saved as a batch file it will not execute.
The script line is:
FOR /R %f IN (*.txt.*) DO REN "%f" *.&
I have attempted to add various triggers from other scripts to get this working but when I get this working via CMD it still will not execute from a batch file.
Can anyone help please.
Mike
Change
FOR /R %f IN (*.txt.*) DO REN "%f" *.&
to
FOR /R %%f IN (*.txt.*) DO REN "%%f" *.&
https://ss64.com/nt/for_f.html
Use double percent sign before your variable name.
Batch script
#echo off
FOR /R HERE_YOUR_ROOT_PATH %%f IN (*.txt.*) DO REN "%%f" *.&
#echo on
This question already has answers here:
Check if any type of files exist in a directory using BATCH script
(4 answers)
Closed 5 years ago.
i want to write a batch script where i need to check if a directory is empty or not, if not empty i want to clean that particular directory. Path will passed via command line argument.
Here's some example code to get you started:
#Echo Off
If "%~1"=="" GoTo :EOF
If Not Exist "%~1\" GoTo :EOF
Dir/B/A "%~1\*"|Find /V "">Nul&&Call :CleanIt
Pause
GoTo :EOF
:CleanIt
Rem Your clean command(s) here
You should only need to add your specific code to the bottom
This question already has answers here:
Get directory name from path of %CD%
(4 answers)
Closed 5 years ago.
I have a simple batch file that I use to archive files in tar/gzip format. I have placed the batch file in system32 so that I can access it from anywhere.
I open the command window using "shift + right click" in a particular folder where I want the contents of the folder to be archived and enter the name of the batch file (targz.bat). Batch file does the archiving/compressing.
The problem is I use absolute paths. I need a way to get the current directory and the name of the current folder. I can get the current directory with %cd%, but how do I get the folder name?
For example:
set currentdir=%cd% "C:\xampp\htdocs\wordpress"
set currentfoldername= should be just "wordpress"
Actual code:
#echo off
cd "C:\Program Files\7-Zip"
7z a -ttar "C:\xampp\htdocs\wordpress\archive.tar" "C:\xampp\htdocs\wordpress\*"
7z a -tgzip "C:\xampp\htdocs\wordpress\archive.tar.gz" "C:\xampp\htdocs\wordpress\archive.tar"
del "C:\xampp\htdocs\wordpress\archive.tar"
exit
Desired:
#echo off
set currentdir=%cd%
set currentfoldername=
cd "C:\Program Files\7-Zip"
7z a -ttar "%currentdir%\%currentfoldername%.tar" "%currentdir%\*"
7z a -tgzip "%currentdir%\%currentfoldername%.tar.gz" "%currentdir%\%currentfoldername%.tar"
del "%currentdir%\%currentfoldername%.tar"
exit
Using the ~n modifier you can easely get the last element of a path :
for %%a in (%cd%) do set "currentfoldername=%~na"