This question already has answers here:
How to test if a file is a directory in a batch script?
(23 answers)
Closed 6 years ago.
I have a batch file script that needs to verify if the directory that the user enters in is a real directory or path so that I can create it if it dose not exist.
:GetDir
set /p Dir=
if not <is directory?> %Dir% goto:GetDir
if not exist %Dir% mkdir %Dir%
How can I determine if the value given by the user is a directory?
There are millions of things that the user could enter in that is obviously not a directory.
notes for editor:
This note is to be removed after the editor reads it. I apologize ahead of time if I was not supposed to add this note in. Please correct me so I don't do this again if this is the case and I would appreciate being told where to add these types of notes if a similar situation comes up again. I am trying to maintain and recover this question from when I originally asked it in order to fix my bad reputation for asking bad questions on here. This is not a duplicate question because the marked duplicate is in a different scripting language that I am unfamiliar with. I would also like to mention that I did to some heavy research before asking this question on the the issue that I was having. It would also be greatly appreciated if answering this question could be enabled again so that I can answer my own question because I am not sure that anybody else is going to have the answer, and I currently know how to answer it.
How do I test if a file is a directory in a Batch script ?
#echo off
set VAR="C:\Program Files"
FOR %%i IN (%VAR%) DO IF EXIST %%~si\NUL ECHO %VAR% It's a directory
pause
You can use this to test if it is specifically a directory (and not a file)
#echo off
set "file=C:\someDirectory"
if exist "%file%\." echo directory
pause
Here's a way (two ways, really) to determine if a directory exists, and that it's actually a directory and not a file.
set /p "ServerMods=Enter a Directory: "
REM First, make sure the input ends with a backslash (\)
if %ServerMods:~-1% NEQ "\" set "ServerMods=%ServerMods%\
REM Option 1
if not exist %ServerMods% goto :GetDir
REM Option 2
dir %ServerMods% 1>nul 2>nul || (goto :GetDir)
if exist "%ServerMods%\." goto GetDir
Related
For executables like EXE, MSI and MSP I am able to detect file type by reading its few bytes. But I am not able to detect BATCH file by reading its content. User can change the BATCH file extension so I don't want to rely on extension to detect a BATCH file. Is there a way to detect BATCH file by reading its content programmatically?
Any help would be greatly appreciated.
(Note:As Compo said that there isn't a clear way of doing this without a sophisticated algorithm and also this question would have been much better at superuser or another platform(and some of you might not like this but future viewer's can get some help from it and it's ok with a downvote --the message will be deleted by me in a while).
Here there is a batch file that might help you with your program(not exactly your answer).
#echo off
rem probably the batch file isn't well written.you can add commands for better detection
rem and also might have great limitations
rem improvements are welcomed
set file=%1
findstr /C:"#echo off" /C:"set" /C:"set /p" /C:"pause" /C:"echo" /C:"pause >nul" /C:"cd" /C:"call" /C:"goto" /C:"if" /C:"type" /C:"rem" /C:"copy" /C:"move" /C:"cls" /C:"wmic" %file% >nul
if %errorlevel%==0 (
echo Appears to be a batch file
) else (
echo Doesn't appear to be a batch file
)
This question already has answers here:
How do I get current date/time on the Windows command line in a suitable format for usage in a file/folder name?
(30 answers)
Format date and time in a Windows batch script
(37 answers)
Time is set incorrectly after midnight
(4 answers)
Closed 2 years ago.
Can someone please help me figure out the syntax to add the time to an output file's name? I was able to get the date, but the time isn't working for me. I keep googling it but so far I have come up empty. I have figured out how to add it to the file itself, but I want to be able to see it in the name.
The code below, is just a super simple robocopy command that I have saved as test.cmd.
Everything works, everything except adding the time.
Feel free to steal it if you want. Just make sure you Google the switches so that you know what you're doing. That part is easy to find.
My Code
#ECHO OFF
if not exist "C:\Example" mkdir "C:\Example"
ROBOCOPY.EXE C:\Example Source "\\192.168.0.100\Shared Storage" /E /Z /J /NOOFFLOAD /R:2 /W:1 /REG /UNILOG+:"C:\Example Source\%date:~-10,2%-%date:~-7,2%-%date:~-4,4% Time.txt" /MT:4
exit
I have figured it out!
Here it is if someone else needs it.
#ECHO OFF
if not exist "C:\Example" mkdir "C:\Example"
ROBOCOPY.EXE C:\Example Source "\192.168.0.100\Shared Storage" /E /Z /J /NOOFFLOAD /R:2 /W:1 /REG /UNILOG+:"C:\Example Source\date %date:~-10,2%-%date:~-7,2%-%date:~-4,4% time %time:~0,2%.%time:~3,2%.txt" /MT:4
exit
This question already has answers here:
Order and move files into directories based on some filenames pattern
(2 answers)
Closed 5 years ago.
I currently have over 100.000 files (.bak) which I need to move 1000 files to a different directory. The files that need to be moved have a twin-file with a different extension (.xml) so finding them should be easy, but for the life of me I can't figure out how to do this. I have no experience with .bat-files and I've already been struggling with stuff for a day. Can someone help me out please?
Quick example:
First file:
File1thatneedstomove.bak
File1thatneedstomove.xml
File1thatdoesntneedstomove.bak
File2thatdoesntneedstomove.bak
File3thatdoesntneedstomove.bak
File2thatneedstomove.bak
File2thatneedstomove.xml
So I need to move the 1st and 6th file to a different folder because they have a twin file where just the text behind the period is different.
Not tested:
set "source_dir=C:\baks"
set "destination=C:\dest"
for %%a in ("%source_dir%\*bak") do (
if exist "%%~dpna.xml" (
echo move /y "%%~fa" "%destination%"
)
)
It will echo the needed move command parameters. If it is ok remove echo word in the line in the brackets.
If I have a directory with 2 files in it like these:
123456789_File1.pdf
123456789_File2.pdf
I need to run a batch file that lets me read the name of the file only and not it's path. Create another file with the same name and a different extension with a contents that is only the first 9 characters of the name. Below is what I have tried and I cannot figure out why this is not correct? I have done several hours of research over something that seems like it should be simple and I feel like I am just missing something.
FOR /R C:\Test\Files %%F in (*.*) do (set content=%%~nF & echo %content:~0,9%>> %%~nF.txt)
This works for me:
#echo off
Setlocal EnableDelayedExpansion
FOR /R . %%F in (*.*) do (
set content=%%~nF
echo !content:~0,9! %%~nF.txt
)
For future reference, please note that "this is not correct" is not an accurate description of the problem as far as StackOverflow questions and answers are concerned. The precise description of your problem is that in each iteration of the loop, %content% appears to contain the exact same content as it had in the first iteration of the loop, regardless of the value of %%F.
The solution is to enable "delayed expansion" and to use the special ! delimiter instead of % so as to enjoy the benefits of delayed expansion.
I am trying to write a log that a user will create in a batch file, as a small little game, but I can't seem to get the correct directory of the file down. I want to write to this directory:
BatchfileandFolder\subfolder1\subfolder2\ThedataisHere.txt
This is the code I have (I really don't know what I am doing)
Echo Write a piece of text here
set /p UserData=
>>\subfolder1\subfolder2\"ThedataisHere.txt" echo %UserData%
It is essential trying to dig deeper into the directory, but I don't know the exact command, and the help menu for CD, and PATH on the cmd.exe promt don't really help me that much.
Thank you for real human contact, speaking real English
#ECHO OFF
SETLOCAL
SET "logfile=u:\sub folder1\sub folder2\ThedataisHere.txt"
FOR /f "delims=" %%a IN ("%logfile%") DO MD "%%~dpa"
Echo Write a piece of text here
set /p UserData=
>>"%logfile%" echo %UserData%
GOTO :EOF
This method uses a variable logfile so that you aren't forever typing out the name (and avoid the pain if you want to change the names or directories, and the method allows you yo use multiple logfiles easily if you want)
I've deliberately used spaces in directory names to prove the method. The directory gets created immediately after the logfile name is set
append 2>nul to the md instruction line to suppress the directory already exists message
From there, simply use >>"%logfile%" to create the log. The quotes are not required if the filename doesn't contain separators like spaces.
Note that if the first character of the directory specified is \ then the directory is relative to the root, but if it is not then the directory is relative to the current directory on the destination drive. u: is a drive specifier, not a directoryname; I use u: as my test drive. Your choice is up to you.
If the directory structure already exists, it's pretty simple:
#echo off
set /p UserData=Write some text here:
#echo %UserData% >> "subfolder1\subfolder2\TheDataIsHere.txt"
If it doesn't exist already, you have to create it first (tested on Win7 64 bit):
#echo off
if not exist "subfolder1" md "subfolder1"
if not exist "subfolder1\subfolder2" md "subfolder1\subfolder2"
set /p UserData=Write some text here:
#echo %UserData% >> "subfolder1\subfolder2\TheDataIsHere.txt"
Although your description is ample, some important points are missing. It is obvious that you know "How to change the directory that a batch file will output text to", because you use the >> \subfolder\... notation, so this is not your problem. I can only guess that you want to know "How to get the directory where a batch file is located", so you can write to a log file placed two levels below that directory. If this is your problem, then you may use the %~P0 notation, that represent the path of the batch file; that is:
>> "%~P0subfolder1\subfolder2\ThedataisHere.txt" echo %UserData%
Note that the value returned by %~P0 ends in a backslash, so %~P0 must not be separated by an additional backslash from subfolder1; also note that the quotes must enclose the whole path of the file.
If this is not what you want, please carefully describe your real problem. Anyway, try to be clearer in future questions.