Batch Script for Check and Cleanup [duplicate] - batch-file

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

Related

using windows batch script to automate unzip task [duplicate]

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%"

FOR command not executing in script [duplicate]

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

How to make a batch file put every file name in a folder in a different variable with for loop [duplicate]

This question already has answers here:
DIR output into BAT array?
(2 answers)
Closed 7 years ago.
There is a folder called 'weapons', and in this folder are empty files, like 'P250.wep, AWP.wep'. I need to make a batch file that loops through the folder, takes every file name, and puts it in a different variable. For example, after looping these files, P250.wep should be stored in variable weapon1, and AWP.wep in weapon2. Please help me!!!
You can try this method, but it's automatically sort alphabetically before the loop starts. So, this mean AWP.wep will be weapon1, and P250.wep will be weapon2. One more thing, I don't know which one do you want to store, you mentioned "take every file name", but you also mentioned file name with extension... If you don't mind, take a look at the script:
#echo off
Setlocal EnableDelayedExpansion
set count=1
for %%a in (c:\PATH\weapons) do (
set "weapon!count!=%%~na"
set /a count+=1
)
pause >nul

Put a command in a variable [duplicate]

This question already has answers here:
Assign output of a program to a variable using a MS batch file
(12 answers)
Closed 7 years ago.
I want to store this command in a variable called code1
dir /a:d /b
so the output would be:
Folder1
Folder2
Folder3
etc...
Any ideas how to do it?
I've checked other questions and when it just sais echo is off.
Simply store the command in the variable like you say. To execute, simply expand the value:
#echo off
setlocal
:: Define a simple "macro"
set "code1=dir /a:d /b"
:: Execute the "macro"
%code1%

Batch checking if a file exists or not [duplicate]

This question already has answers here:
How to verify if a file exists in a batch file?
(3 answers)
Closed 8 years ago.
I need a batch code that will tell me if test.txt is there or not it is in the same directory as the batch file. I would like to know what I should put in the space where it says the code. Can anyone help?
rem the code
echo yes
rem the code
echo no
IF EXIST yourfilename (
echo Yes
) ELSE (
echo No
)
Note : ELSE command must be on the same line as the end of the IF command.
To explore more type IF /? in cmd.

Resources