I have been tinkering with some batch files to clean up some of my folders and I am stuck.
What I am trying to do is check the directory Installers for any files or folders and then either :goto empty or :goto notempty .
I have spent ages searching for a solution but everything that I have found is to either check if only files exist in a directory or check if a specified folder exists in a directory.
EDIT: This is what I have so far.
#echo off
echo Beginning File Cleanup
echo Installers Start
"C:\Program Files\WinRAR\rar.exe" a -r -df "Installers.rar" Installers
echo Installers Done
echo old Start
"C:\Program Files\WinRAR\rar.exe" a -r -df "old.rar" old
echo old Done
mkdir Installers
mkdir old
pause
The code above works but I only want it to run the rar.exe bit if the folder is empty hence to :goto requirement
Thanks
I must be missing something, but I didn't saw how does you current code relate to what you asked...
You can do what you want with the following line
dir /b "path/to/Installers" | findstr "^" >nul && (echo Folder not empty) || (echo Folder is empty)
Just see what happens at the echo parts and replace for the command you want.
Cheers.
Related
I'm new to this cmd/FTP command. I would like to create a new folder at my local directory using today's date and connect to FTP to download the specific file to the newly created folder. If I manually type in command one by one at cmd, it has no issue. But when I use a batch file to run, my command stopped at FTP.
setlocal enableextensions
set name=%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4"_"job%
mkdir C:\%name%
cd C:\%name%
ftp
open 192.168.31.93
*user*
*password*
binary
cd *directory*
mget -i *.*
I did try to separate my command to two batches;
1. folder creation
2. FTP download but the file downloaded didn't go into the folder I created. the downloaded file went to C:\Document & Settings.
main batch file
#echo off
call rename.bat
ftp -i -s:ftp.txt
rename.bat
setlocal enableextensions
set name=%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4%"_job"
mkdir c:\%name%
cd c:\%name%
ftp.txt
open 192.168.31.93
*user*
*password*
binary
cd *directory*
mget *.*
close
Another method I try is using '!' when in FTP environment, then create a folder then exit back to FTP environment. This method again doesn't work with the batch file. Please help
It seems that with command extensions enabled, the working directory set by a child batch file is lost, then the batch file exits.
I'm not sure how to solve it, but you actually do not need the rename.bat file to be a separate file. This "main batch file" should work:
#echo off
setlocal enableextensions
set name=%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4%"_job"
mkdir c:\%name%
cd /d c:\%name%
ftp -i "-s:%~dp0\ftp.txt"
Also note the /d added to cd. Without that your batch will not work when started from another drive. You also have to use %~dp0 to refer to the batch file folder for the ftp.txt. As at the time ftp is called, you have changed to the target directory.
You possibly do not even need the command extensions to be enabled. So simply removing the setlocal enableextensions might solve the problem too. Though you still need the %~dp0 and /d.
I've decided to post this, although similar to the answer given, there are a couple of differences.
It creates the text file, then deletes it, (this keeps everything more portable).
I have corrected your directory name, (because of a typo).
#Echo Off
Set "Name=%DATE%"
Set "Name=%Name:~-10,2%-%Name:~-7,2%-%Name:~-4%_job"
MD "C:\%Name%" 2>Nul
CD /D "C:\%Name%" || Exit /B
( Echo open 192.168.31.93
Echo *user*
Echo *password*
Echo binary
Echo cd *directory*
Echo mget *.*
Echo close
)>"ftp.txt"
FTP -i -s:ftp.txt
Del "ftp.txt" 2>Nul
Exit /B
Here is my code:
net use v: \\kimezgls-rez\arxiv_mppi
net use w: \\###.###.#.##\Protokoliem\KIMEZGLS-REZ_mppi
cls
cd V:\
if not exist "V:\01_2016" mkdir "V:\01_2016"
pause
ECHO Folder is Created!
ECHO Moving Files...
MOVE "V:\??_01" "V:\01_2016"
ECHO Creating Backup!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
for /d %%X in (01_2016) do "C:\Program Files\7-Zip\7z.exe" a "%%X.7z" "%%X\"
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ECHO Please wait few seconds...
ECHO Backup is Created!
ECHO Sending Backup to ###.###.#.##\Protokoliem\KIMEZGLS-REZ_mppi
MOVE "V:\01_2016.7z" "w:\"
pause
cls
pause
GOTO MENU
I am trying to make script, that will Create directory, name it 01_2016, and put all files, where name ends with _01 in it. Then Archive folder 01_2016 and send to Another Network folder.
Everything went well to the point, where script started to Archive folder 01_2016, but couldn't.
Scanning the drive:
WARNING: The system cannot find the file specified.
01_2016
0 files, 0 bytes
Creating archive: 01_2016.7z
Items to compress: 0
Files read from disk: 0
Archive size: 32 bytes (1 KiB)
Scan WARNINGS for files and folders:
01_2016 : The system cannot find the file specified
----------------
Scan WARNINGS: 1
For some reason it saves empty .zip folder right on my Desktop. While anylyzing whole code, I came to conclusion, that this command is only thing that doesn't want to let me successfully run script.
Thanks for any help
I guess the problem is that CD V:\ is not enough. If you are on C:\ CD V:\ won't move the scope to V:. To achieve this you have to add the /d switch to the CD command:
...
cd /d V:\
...
I'm looking for a batch file to clear a few directories of all files and folders, and make a report on if the directory was empty/if it was cleared.
I've looked online and tried a few suggestions, however none have worked. I can delete files but not folders, plus that doesn't use an IF statement to output if the directory was empty.
The contents of the directory will be random and have unknown files and folder names as we are using them as temporary locations that need to be deleted at the end of every day.
I have tried:
if EXIST "path\to\directory*.* " del "path\to\directory*.* "
But that doesn't delete folders.
Any help would be greatly appreciated!
Thanks
Maybe this Resolves the issue
#Echo off
Echo Beginning Cleaning Process
Echo Please Have patience
if not exist C:\path\to\file\file.exe goto End
if not exist C:\path\to\folder\folder goto End
if not exist C:\path\to\another\folder goto End
DEL C:\path\to\file\file.exe
DEL C:\path\to\folder\folder
DEL C:\path\to\another\folder
ALL Files Have Been Removed, Hopefully
pause
:End
Echo All Files Not Removed
Pause
If you want to remove all the Text files in the directory than you can use
DEL C:\Path\to\Text\file\*.txt
If you want to reomve all files in the folder then you can try this
Lets name the folder xyz which is in C:\True\xyz
RMDIR /S "C:\True\xyz"
MD "C:\True\xyz"
The RMDIR command will remove the directory and all the folders and files in it. Then the MD command will make a new directory with the same name and will obviously will be empty !
I am trying to get a batch file to create a folder on the desktop with a text file inside of it. Every-time i try to run this line of code it gives my the error that "The filename, directory name, or volume label syntax is incorrect."
echo ========================
::CREATE FILES START
cd /d C:
md Title
echo.>"C:\Users\%USERACCOUNT%\Desktop\Example\example.txt"
::CREATE FILES END
echo Done!
pause >nul
Your code is changing to drive C, then creating GeoHunt2015 in root. Then you try to echo the file into non-existent folder on desktop, hence the error.
This assumes your %userprofile% is "c:\users\name"
md "%USERPROFILE%\Desktop\GeoHunt2015"
echo.>"%USERPROFILE%\Desktop\GeoHunt2015\Mission_Instuctions.txt"
or you can cd to desktop
echo ========================
:: CREATE FILES START
cd /d "%USERPROFILE%\Desktop\"
md GeoHunt2015
echo. >"GeoHunt2015\Mission_Instructions.txt"
:: CREATE FILES END
echo Done!
pause >nul
Is %USERACCOUNT% defined?
Is the echo actually causing the issue?
Try commenting out stuff until you are sure that the echo is causing the syntax error.
A couple things I can see. You're switching to the C: directory, then making the GeoHunt2015 folder, but then attempting to echo into the GeoHunt2015 folder on your desktop.
Try this echo instead:
echo.>"C:\GeoHunt2015\Mission_Instructions.txt"
Try using
mkdir "C:\%USERPROFILE%\Desktop\GeoHunt2015"
echo.>"C:\%USERPROFILE%\Desktop\GeoHunt2015\Mission_Instuctions.txt"
as you had in your original version of the question.
I am trying to unzip some folders and then recombine them, however when I run the batch file it get the following error:
'unzip' is not recognized as an internal or external command,
operable program or batch file.
I am guessing I need to point the batch file to where 7-zip is correct? Any insight would be appreciated thanks!
See below for code:
#echo off
cls
:start
:unzip
echo.
echo Unzipping files
echo.
unzip ELMDB-85308-PI003_OVA_2of5.zip
unzip ELMDB-85308-PI003_OVA_3of5.zip
unzip ELMDB-85308-PI003_OVA_4of5.zip
unzip ELMDB-85308-PI003_OVA_5of5.zip
echo.
:combining
echo.
echo Combining parts
echo.
copy /b ELMDB-85308-PI003_OVA_1of4+ELMDB-85308-PI003_OVA_2of4+ELMDB-85308-PI003_OVA_3of4+ELMDB-85308-PI003_OVA_4of4 ELMDB-85308-PI003.OVA
:done
echo.
echo Done!
echo.
pause
:end
Instead of unzip as a command, try 7z -e.
If you copy 7z.exe to some directory that is on your PATH, then you don't need to specify the directory.
You can see your PATH by executing
PATH
from the prompt. This displays a ;-separated list of directories that are searched after the current directory) for executables.
If 7z.exe is not on your PATH, then you'd need
"c:\wherever\it is\7z" -e ....
(more explaining)
If 7z is in your path, then
7Z -e ELMDB-85308-PI003_OVA_2of5.zip
will unzip ELMDB-85308-PI003_OVA_2of5.zip
And you'd then need to add a line for each of the other files (which you have done).
If 7z.exe is not on your path then you need
"C:\Program Files\7-Zip\7z" -e ELMDB-85308-PI003_OVA_2of5.zip
and reproduce that for each of your files.
You can tell that 7z.exe is in your path by executing 7z from the prompt. You will get either a report stating that it's not recognised (ie it's not on your path) or you'll get a commands-and-switches help report (which means it is on your path)
Since the command 7z did not work, then it's not on your path, so you need the other form.
If you were to copy /b C:\Program Files\7-Zip\7z.exe"c:\windows\system32\" then in all probability you'd have 7z.exe on your path and hence you could use the short form.