i want to make an IF NOT EXIST for some files like
IF NOT EXIST *"%input%*.jpg" && "%input%*.png"* (
ECHO.=========================================
ECHO.= ERROR : PUT UR IMAGE FILE TO /input =
ECHO.=========================================
pause
goto MENU
) ELSE (
ECHO.=========================================
ECHO.= Listing file =
ECHO.=========================================
cd %input%
dir /B
ECHO.=========================================
ECHO.= listing done =
ECHO.=========================================
timeout /T 2 \> nul
cls
but it says
=========================================
= ERROR : PUT UR IMAGE FILE TO /input =
=========================================
i already put them all png and jpg files but i think i do somenthing wrong :O
If you want to check if both files are present, I would use:
IF EXIST "%input%*.jpg" if exist "%input%*.png" goto :ok
echo at least one of the files is missing.
goto :eof
:ok
echo do something with both files here
The second if is only executed, when the first if finds the first file
#echo off
if exist %1\*.jpg goto :ok
if exist %1\*.png goto :ok
echo no JPG or PNG files found in %1
goto :EOF
:ok
echo found JPG and/or PNG files in %1
If you are looking for files in a subdirectory, you need to remember to use the directory separator (either \ or /) between the directory name and the match pattern.
For testing against multiple different file types, just use multiple ifs.
By the way, you seem to have an extra \ there on the penultimate line, which should read: timeout /t 2 > nul.
Related
I have a game that I play and mod a lot, and a lot of the files in the game have file extensions that are in all caps, which bothers me quite a bit. I'm trying to change them all to be lowercase, but there are numerous folders in the game files, so I'm having to be very repetitive. Right now, I'm working with this:
cd\program files (x86)\Activision\X-Men Legends 2\Actors
start ren *.IGB *.igb
cd\program files (x86)\Activision\X-Men Legends 2\Conversations\
start ren *.XMLB *.xmlb
cd\program files (x86)\Activision\X-Men Legends 2\Conversations\act0\tutorial\tutorial1
start ren *.XMLB *.xmlb
and so on for each and every folder in the game files. I have a very long .bat file where I just have line after line of this but with a different destination folder. Is there a way to streamline this process so I don't have to manually type out each folder name? Also, is there a line that I could add at the beginning to automatically run as an administrator, so I don't have to make sure to run the .bat file as an administrator each time?
I'm not looking for anything complicated, and I'm very inexperienced with coding other than the small amount of stuff I've been able to search up.
Instead of doing it for each folder, use a for /R loop which loops through all subfolders. I would suggest the following code:
#echo off
:prompt
set /p "extensions=What are the up-case extensions you want to convert to lower-case?: "
if not defined extensions (cls & goto:prompt) else (goto:loop)
:loop
for %%A IN (%extensions%) do (
for /R "custom_folder" %%B IN (*.%%A) do (
ren "%%~fB" "%%~nB.%%A"
)
)
Take a look on this on how to run this batch file as admin. Create another batch file and add the code specified in the accepted answer.
Note: As Stephan pointed out in the comments, you can use %ProgramFiles(x86)% environment variable which is the same thing.
#echo off
setlocal
rem Check if admin.
2>nul >nul net session || goto :runasadmin
rem Start in script directory.
pushd "%~dp0" || (
>&2 echo Failed to change directory to "%~dp0".
pause
exit /b 1
)
rem Ask for directory to change to, else use the script directory if undefined.
set "dirpath=%~dp0"
set /p "dirpath=Dir path: "
rem Expand any environmental variables used in input.
call set "dirpath=%dirpath%"
rem Start in the input directory.
pushd "%dirpath%" || (
>&2 echo Failed to change directory to "%dirpath%".
pause
exit /b 1
)
rem Ask for file extensions.
echo File extensions to convert to lowercase, input lowercase.
echo i.e. doc txt
set "fileext="
set /p "fileext=File extension(s): "
if not defined fileext (
>&2 echo Failed to input file extension.
pause
exit /b 1
)
rem Display current settings.
echo dirpath: %dirpath%
echo fileext: %fileext%
pause
rem Do recursive renaming.
for %%A in (%fileext%) do for /r %%B in (*.%%A) do ren "%%~B" "%%~nB.%%A"
rem Restore to previous working directory.
popd
echo Task done.
pause
exit /b 0
:runasadmin
rem Make temporary random directory.
set "tmpdir=%temp%\%random%"
mkdir "%tmpdir%" || (
>&2 echo Failed to create temporary directory.
exit /b 1
)
rem Make VBS file to run cmd.exe as admin.
(
echo Set UAC = CreateObject^("Shell.Application"^)
echo UAC.ShellExecute "cmd.exe", "/c ""%~f0""", "", "runas", 1
) > "%tmpdir%\getadmin.vbs"
"%tmpdir%\getadmin.vbs"
rem Remove temporary random directory.
rd /s /q "%tmpdir%"
exit /b
This script is expected to start from double-click.
It will restart the script as admin if not already admin.
It will prompt to get information such as directory to change to and get file extensions i.e. doc txt (not *.doc *.txt). If you enter i.e. %cd% as the directory input, it will be expanded.
Hi I would like to create a batch file which finds certain keywords in .eml files (destination A) and then deletes the line in which they reside. After that I need the batch file to put the "new" files in separate .eml files in (destination B). the files can be .txt as well.
e.g
line 2 needs to be removed which I can do using findstr, however my problem is that after I get the lines removed I can only place the "new file" in one .txt file and I need to place the "new files" in multiple .txt files in the same destination.
`e.g
DESTINATION A "NEW FILE" DESTINATION B
line1: good line1: good File1.txt
line2: error > line2: good > File2.txt
line3: good File.... to however many "new files" i have.`
I have searched for a forcedir type command by I had no luck.
here is the code I use:
`findstr /v /I "2 3 7" C:\A\*.txt >> C:\B\onefileonly.txt
msg * Done!
exit >nul`
<----- this onefileonly.txt is my problem. I need it to be the seperate "new folders".
Inside the onefileonly.txt file
I have also tried this code, however I has the same problem.
`#echo off
echo Removing...
for /f "skip=3 delims=*" %%a in (C:\A\testfile.txt) do (
echo %%a >>C:\B\onefileonly.txt
) >nul
echo Lines removed, rebuilding file...
xcopy C:\B\onefileonly.txt C:\A\testfile.txt /y >nul
echo File rebuilt, removing temporary files
del C:\B\onefileonly.txt /f /q >nul
msg * Done!
exit >nul`
Ok so after about a week i managed to bash together a script which answers this question, I still need to clean it up a bit but here is the code below
::CallScript
#echo off
CALL :ScriptA
CALL :ScriptB
CALL :ScriptC
pause
goto :eof
:ScriptA
del "C:\source\INCOMPLETE MESSAGE*.eml" "C:\source\EXCEPTION ERROR*.eml"
goto :eof
:ScriptB
#echo off
SETLOCAL
FOR %%i IN (C:\source\*.eml) DO (
TYPE "%%i"| more /E +4 >> C:\5500\%%~ni.eml
)
goto :eof
:ScriptC
del "C:\source\ERROR WITH Position Post*.eml"
goto :eof
The script deletes the unwanted email files in source (which are INCOMPLETE MESSAGE & EXCEPTION ERROR )
Then the script takes the email files out of source, removes the top 4 lines in the .eml document copies them to the 5500 folder and lastly deletes the old .eml files in the source folder.
I need to toggle the names of two specific files existing at a specific path on the local drive and wonder if a .bat or a .vbs can achieve that.
In other words executing the script once swaps "File A" with "File B" ("C:\Path A\File A.txt" with "C:\Path A\File B.txt") and running it once more will swap them again.
I am also curious to know if the same can be done:
1) In this situation -->> "C:\Some Path\File A.txt" and "D:\Some Other Path\File B.txt"
and
2) If instead of two files I want to toggle two folders.
Create the following batch file and name it whatever you want. I'm using the name "myRename.bat".
:: myRename.bat
#echo off
SETLOCAL
:: verify the first file exists
if not exist "%~1" ( echo ERROR: File not found "%~1" & goto endofscript )
:: verify the second file exists
if not exist "%~2" ( echo ERROR: File not found "%~2" & goto endofscript )
:: Create a guaranteed unique string for temporarily naming one file
set instance=%date:~-4,4%%date:~-10,2%%date:~-7,2%
set instance=%instance%-%time:~0,2%%time:~3,2%%time:~6,2%%time:~9,2%
set instance=%instance%-%RANDOM%
:: rename the first file to a temporary name
ren "%~1" "%~nx1.%instance%"
:: rename the second file to the first file name
ren "%~2" "%~nx1"
:: rename teh first file to the second file name
ren "%~1.%instance%" "%~nx2"
:endofscript
Assuming these two files exist in this path:
c:\temp\Rename test\File A.txt
c:\temp\Rename test\File B.txt
Then you can run the command below and they will swap names:
myRename "c:\temp\Rename test\File A.txt" "c:\temp\Rename test\File B.txt"
If either File A or File B are not found, that error is reported on screen and the process stops.
I am trying to write a bat file for a network policy that will install a program if it doesn't exist as well as several other functions. I am using GOTO statements depending on whether or not certain criterion are met. However, it seems that the labels are not firing correctly as all of them do.
I have simplified my script so as to grasp some idea of what may be happening.
#echo off
IF EXIST c:\test\test.txt (GOTO :EXISTING) ELSE GOTO :MISSING
:EXISTING
echo file exists
:MISSING
echo file missing
ping localhost -n 5 >NUL
Basically it checks to see that the file "test.txt" exists in folder "c:\test" which id does. So it should echo file exists to the console. However, both "file exists" and "file missing" are echoed to the console. I find that if I remove the file from the folder or simply rename it, it only echoes "file missing"
Why is it running running both labels?
Because a GOTO is just a jump in execution to a point in the script, then execution continues sequentially from that point. If you want it to stop after running 'EXISTING', then you need to do something like this. Note the extra GOTO and new label:
#ECHO OFF
IF EXIST c:\test\test.txt (GOTO :EXISTING) ELSE GOTO :MISSING
:EXISTING
echo file exists
goto :NEXTBIT
:MISSING
echo file missing
:NEXTBIT
ping localhost -n 5 >NUL
It's worth noting though that with cmd.exe (i.e., the NT-based command shells [NT, Win2k, XP, etc]), you can do IF...ELSE blocks like this:
#ECHO OFF
IF EXIST c:\test\test.txt (
ECHO File exists
) ELSE (
ECHO File missing
)
ping localhost -n 5 >nul
...so you can eliminate your GOTOs entirely.
It's because you need to skip over the "missing" bit if it exists:
#echo off
IF EXIST c:\test\test.txt (GOTO :EXISTING) ELSE GOTO :MISSING
:EXISTING
echo file exists
goto :COMMON
:MISSING
echo file missing
:COMMON
ping localhost -n 5 >NUL
You may also want to keep in mind that the current cmd.exe batch language is a fair bit more powerful than that which came with MS-DOS. I would prefer this one:
#echo off
if exist c:\test\test.txt (
echo file exists
) else (
echo file missing
)
ping localhost -n 5 >nul
After you echo file exists the next command is
echo file missing
You need to do something to skip the missing case. Perhaps another goto to a :PING label?
When you're debugging it helps to keep the echo on.
Because GOTO statement moves the execution to that label. To use it in the situation like yours, you need to add another GOTO label.
#echo off
IF EXIST c:\test\test.txt (GOTO :EXISTING) ELSE GOTO MISSING
:EXISTING
echo file exists
GOTO END
:MISSING
echo file missing
GOTO END
:END
ping localhost -n 5 >NUL
#echo off
IF EXIST "c:\test\test.txt" ( :: warning double quotes
GOTO EXISTING
) ELSE ( :: this format best in batch
GOTO MISSING
) :: don't forget
:EXISTING
echo file exists
goto OTHER :: if file exist jump OTHER
:MISSING
echo file missing
:: label is not required
:OTHER
timeout /t 5 >nul
pause
How would one go best about checking for existence of all files before building?
Let me explain; I mostly build stuff from the command prompt. No problems there, just put the build command and all in one .bat /.cmd file, and run it. It works fine.
But, for the normal running of my program, for example, I need several source files for the build, and then an additional few data files, measured data and such.
Is there a way to test via a batch file whether a file exists, and if it exists just write OK?
file1.for OK
file2.for OK
datafile.txt OK
data.dat MISSING FROM DIRECTORY
How could this be accomplished?
As a slightly more advanced approach:
#Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
Set FileList=file1.for file2.for "File with spaces" ...
Set Build=1
For %%f In (%FileList%) Do Call :FileExists %%f
If Not Defined Build (
Echo.
Echo Build aborted. Files were missing.
GoTo :EOF
)
...
GoTo :EOF
:FileExists
Set FileName=%~1
If Exist "!FileName!" (
Echo !FileName! OK
) Else (
Echo !FileName! MISSING FROM DIRECTORY
Set Build=
)
GoTo :EOF
You can put all files into the FileList variable. The Build variable controls whether to continue with the build. A single missing file causes it to cancel.
Something like this?
#ECHO OFF
IF EXIST "c:\myfile1.txt" (ECHO myfile1.txt OK) ELSE (ECHO myfile1.txt FILE MISSING FROM DIRECTORY)
IF EXIST "c:\myfile2.txt" (ECHO myfile2.txt OK) ELSE (ECHO myfile2.txt FILE MISSING FROM DIRECTORY)
For a list of available commands, see http://ss64.com/nt/