I am trying to find and replace values of a string within a batch file but having issues. I have the user run the batch file and it asks the user 1)what drive the file is on 2)what is the name of folder in the TEST parent folder 3)what is the name of the new server. I want the batch file to look within a file called importer.config and replace a value called server_name with whatever the input from the user is. Here is what I have:
#echo off
SET drive=
SET /P drive=Please enter the drive:
SET folder=
SET /P folder=Enter name of folder desired:
SET server=
SET /P server=Enter name of new server:
#echo off > newfile.txt
setLocal EnableDelayedExpansion
if exist newfile.txt del newfile.txt
for /f "tokens=* delims= " %%a in (%drive%\test\%folder%\importer.config) do (
set str=%%a
set str=!str:server_name=%server%!
echo !str! >> newfile.txt
)
del importer.config
rename newfile.txt importer.config
pause
Every time I run this, the cmd prompt shows:
The system cannot find the file specified c:\test\users_input_they_entered\importer.config. The issue is that file is there so trying to understand what I am missing and why it cant find the file that does exist.
It then also states "Could not find c:\windows\system32\importer.config" which not sure why that happens as well
I have searched on stackoverflow, but cannot figure this out with any assistance.
You're pushing your luck using the for loop for that.
A tool like sed would work well.
If you look at this post they have a vbscript implementation that you could use
Is there any sed like utility for cmd.exe
set input_file=importer.config
set output_file=temp.config
set new_server_name=server1984
cscript /Nologo sed.vbs s/server_name/%new_server_name%/ < %input_file% > %output_file%
Related
I have written a batch file that I use for file management. The batch file parses an .XML database to get a list of base filenames, then allows the user to move/copy those specific files into a new directory. The program prompts the user for a source directory and the name of the .XML file. I would like the program to default the variables to the last used entry, even if the previous CMD session has closed. My solution has been to ask the user for each variable at the beginning of the program, then write those variables to a separate batch file called param.bat at the end like this:
#echo off
set SOURCEDIR=NOT SET
set XMLFILE=NOT SET
if exist param.bat call param.bat
set /p SOURCEDIR=The current source directory is %SOURCEDIR%. Please input new directory or press [Enter] for no change.
set /p XMLFILE=The current XML database is %XMLFILE%. Please input new database or press [Enter] for no change.
REM {Rest of program goes here}
echo #echo off>param.bat
echo set SOURCEDIR=%SOURCEDIR%>>param.bat
echo set XMLFILE=%XMLFILE%>>param.bat
:END
I was hoping for a more elegant solution that does not require a separate batch file and allows me to store the variable data within the primary batch file itself. Any thoughts?
#echo off
setlocal
dir /r "%~f0" | findstr /c:" %~nx0:settings" 2>nul >nul && (
for /f "usebackq delims=" %%A in ("%~f0:settings") do set %%A
)
if defined SOURCEDIR echo The current source directory is %SOURCEDIR%.
set /p "SOURCEDIR= Please input new directory or press [Enter] for no change. "
if defined XMLFILE echo The current XML database is %XMLFILE%.
set /p "XMLFILE=Please input new database or press [Enter] for no change. "
(
echo SOURCEDIR=%SOURCEDIR%
echo XMLFILE=%XMLFILE%
) > "%~f0:settings"
This uses the Alternate Data Stream (ADS) of the batchfile to
save the settings.
NTFS file system is required. The ADS stream is lost if the
batchfile is copied to a file system other than NTFS.
The dir piped to findstr is to determine if the
stream does exist before trying to read from it.
This helps to avoid an error message from the for
loop if the ADS does not exist.
The for loop sets the variable names and values read from the ADS.
Finally, the variables are saved to the ADS.
Note:
%~f0 is full path to the batchfile.
See for /? about all modifiers available.
%~f0:settings is the batchfile with ADS named settings.
dir /r displays files and those with ADS.
Important:
Any idea involving writing to the batchfile could result
in file corruption so would certainly advise a backup of
the batchfile.
There is one way to save variables itself on the bat file, but, you need replace :END to :EOF
:EOF have a good explained in this link .:|:. see Where does GOTO :EOF return to?
Also, this work in fat32/ntfs file system!
You can write the variables in your bat file, and read when needs:
Obs.: Sorry my limited English
#echo off & setlocal enabledelayedexpansion
set "bat_file="%temp%\new_bat_with_new_var.tmp"" & type nul >!bat_file! & set "nop=ot Se"
for /f %%a in ('forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo 0x40"') do set "delim=%%a"
type "%~f0"| findstr "!delim!"| find /v /i "echo" >nul || for %%s in (SOURCEDIR XMLFILE) do set "%%s=N!nop!t"
if defined SOURCEDIR echo/!SOURCEDIR!%delim%!XMLFILE!%delim%>>"%~f0"
for /f "delims=%delim% tokens=1,2" %%a in ('type "%~f0"^| findstr /l "!delim!"^| find /v /i "echo"') do (
set /p "SOURCEDIR=The current source directory is %%~a. Please input new directory or press [Enter] for no change: "
set /p "XMLFILE=The current XML database is %%~b. Please input new database or press [Enter] for no change: "
if /i "!old_string!" neq "!SOURCEDIR!!delim!!XMLFILE!!delim!" (
type "%~f0"| findstr /vic:"%%~a!delim!%%~b!delim!">>!bat_file!"
copy /y !bat_file! "%~f0" >nul
echo/!SOURCEDIR!!delim!!XMLFILE!!delim!>>%~f0"
goto :_continue_:
))
:_continue_:
rem :| Rest of program goes here | replace/change last command [goto :END] to [goto :EOF]
goto :EOF
rem :| Left 2 line blank above, because your variable will be save/read in next line above here |:
I have a question. Is it possibile in batch language to search in folder a part of name that is same like another file and display it.For example i got folder with files :
ggggggsss.mp3
ddddddeee.mp3
ddddddff.mp3
ssssssddd.mp3
aaaaasssss.mp3
11111ssdas.mp3
11111dddd.mp3
...
I need to display in cmd only names of files
ddddddeee
ddddddff
and
11111ssdas
11111dddddd
Because the first six letter are the same. Could someone help me with this problem?
Save this script to test.bat and run from open Cmd Prompt. Replace dir value with path to your folder with .mp3 files:
#echo off
setlocal enabledelayedexpansion
set "dir=%userprofile%\music"
set "pattern1=dddddd" & set "pattern2=11111"
pushd "%dir%"
FOR %%G IN (*.mp3) DO ( set song=%%G
if "!song:~0,6!"=="%pattern1%" echo %%G)
echo/
FOR %%G IN (*.mp3) DO ( set song=%%G
if "!song:~0,5!"=="%pattern2%" echo %%G)
popd
exit /b
See also Extract Substrings.
I have a script that I can run in the command line to get me the version of software. It works perfectly in the command line. I type this in getversion "<full path>" and it gets me exactly what I need.
Now the catch is that I have to have the getversion.bat and a vbscript file both in the directory that I'm in for the command line. This is probably a dumb question but if I want to add this into a batch script where the version is set as a variable how would I do that?
right now I had it looking like this
#echo off
set version=getversion "<full path>"
echo %version%
pause>nul
The problem seems to be that the batch file doesn't know where to find getversion.bat or the vbscript referenced in that script. How can I tell the batch file where they are?
One way to accomplish this is to direct the output of your function to a file and then read this output back into a local variable using a SET /P command. This should do it:
SET TempFile="%Temp%\%RANDOM%.txt"
REM Direct output to a temp file.
CALL getversion "<full path>">%TempFile%
REM Read the output written to the temp file into a local variable.
SET /P Version=<%TempFile%
ECHO %Version%
REM Cleanup.
IF EXIST %TempFile% DEL %TempFile%
Alternately (and this way much "cleaner"), you can use a FOR command to run your function and store the output into a local variable:
FOR /F "usebackq tokens=* delims=" %%A IN (`CALL getversion "<full path>"`) DO SET Version=%%A
ECHO %Version%
I want to copy a file with set /p. Task: Write a file name with set /p and that will be copied in a directory that I want, but it doesn't work.
My current source:
#echo off
echo Enter YOur Pic Name With .jpg
set /p cop=
xcopy /s %systemroot%\%cop% %systemroot%\system32\oobe\info\backgrounds
cls
pause
For the SET /P command, the format works best like this:
set /p cop=Enter your Pic name with .jpg
I'd also suggest you add a couple of lines to check if the file is actually there:
#rem just check that the full path is what I expect
echo %systemroot%\%cop%
#rem and check if the file is there
dir %systemroot%\%cop%
(Delete these lines once your batch file is working).
Also, delete the cls line, until it's working. Then, once it's doing what you need, you can put it back in, if you want.
You might want to think about whether %systemroot% is the right place for these pictures, even temporarily. It's usually used for Windows OS code.
It is possible that %systemroot% path may have spaces, so file names must be enclosed in quotes:
#echo off
set /p "cop=Enter YOur Pic Name With .jpg: "
xcopy /s "%systemroot%\%cop%" "%systemroot%\system32\oobe\info\backgrounds"
cls
pause
try running as admin: http://windows.microsoft.com/en-us/windows7/how-do-i-run-an-application-once-with-a-full-administrator-access-token
also you may try the following (xcopy doesn't like the /s), I think you're trying to silence the output, just try with the >nul redirect:
#echo off
echo Enter YOur Pic Name With .jpg
set /p cop=
xcopy /y %systemroot%\%cop% %systemroot%\system32\oobe\info\backgrounds\* >nul
cls
pause
I want to scan a folder whose path is defined by user input & finally apply ffmpeg to store all media files information into a text file. The following code does not work
#echo off
setLocal EnableDelayedExpansion
set /P "path=Enter folder path: "
dir %path% /B /O:N | findstr ".wmv$ .mpg$ .mkv$ .mpeg$ .mp4$ .avi$" >filename.txt
echo. >info.txt
for /f "tokens=* delims= " %%a in ('type filename.txt') do (
set in=%in%%%a
ffprobe "%path%!in!" 2>>info.txt
)
pause
However if I strip user input as follows the code does work?
#echo off
setLocal EnableDelayedExpansion
::set /P "path=Enter folder path: "
dir d:\Trainers\out\outt\ /B /O:N | findstr ".wmv$ .mpg$ .mkv$ .mpeg$ .mp4$ .avi$" >filename.txt
echo. >info.txt
for /f "tokens=* delims= " %%a in ('type filename.txt') do (
set in=%in%%%a
ffprobe "d:\Trainers\out\outt\!in!" 2>>info.txt
)
pause
The above script is placed in the folder containing ffprobe.exe & it successfully creates two txt files in the same directory
Note that d:\Trainers\out\outt\ is the directory to scan for media files & not the directory from where this bat file is executed
The basic syntax for ffprobe is
ffprobe "videofile"
Use a different variable name than path.
PATH already does something really important. So important, in fact, that your changing it is precisely why the script is breaking.
More specifically, when you try to execute a program using just a filename (no path at all), if the program cannot be found in the working directory, the contents of the PATH environment variable are searched. I haven't seen the error message you're getting, but it's probably failing to execute findstr, which is an executable typically found in a folder specified in PATH.
The user input command looks like the wrong context. Drop the quotes and it should work properly. The quotes are not needed to separate your prompt from your user input.