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%
Related
I'm new to batch scripting and need help here.
My file name along with path is
C:\test\My_Test_File_20201006.txt
and I want to rename it as
C:\test\My_File_20201006.txt
using batch script only. I cannot use PowerShell here.
#echo off
set Pattern="Test_File"
set Replace="File"
Rem accepts the filename as cmd line argument
set filename=%1
Rem Update filename
set targetfile=%filename:Pattern=Replace%
Rem Rename the file
Ren %filename% %targetfile%
Exit
Using the above code, My file is renamed as "File". Tried % around the Pattern & replace variables, but no luck. Not sure where I'm going wrong. Tried all possible solutions from the StackOverflow and other tutorials, but none helped.
Edit:
After the proposed solution, getting a syntax error. The code is as below:
#echo off
set "filename=%~nx1"
echo %filename%
echo "%~dp1"
echo "%~dp1%filename:statement_=%"
ren "%~dp1%filename%" "%~dp1%filename:Test_=%"
I call my script from cmd line as:
D:/Test> C:/script/rename.bat C:\test\My_Test_File_20201006.txt
The echo statement correctly prints filename, directory & filename with the directory. Facing issues in rename statement.
Output:
My_Test_File_20201006.txt
"C:\test\"
"C:\test\My_Test_File_20201006.txt"
The syntax of the command is incorrect.
Three things wrong here.
You cannot add the quotes as part of the variable's value. It will actually use them as part of the variable. set variables to have double quotes including the variable name. For instance instead of set Pattern="Test_File" rather do set "Pattern=Test_File"
You never used the variables you've set Replace and Pattern
You either need to enabledelayedexpansion or use call to do the replacement because of the multple % required.
#echo off
set "Pattern=Test_File"
set "Replace=File"
Rem accepts the filename as cmd line argument
set "filename=%~nx1"
Rem Update filename
setlocal enabledelayedexpansion
ren "%~dp1%filename%" "!filename:%Pattern%=%Replace%!"
Another method, seeing as you only replace Test_ in your example:
#echo off
set "filename=%~nx1"
ren "%~dp1%filename%" "%filename:Test_=%"
EDIT
Fixing your example as per the edit.
#echo off
set "filename=%~nx1"
echo %filename%
echo "%~dp1"
echo "%~dp1%filename:statement_=%"
ren "%~dp1%filename%" "%filename:Test_=%"
I am trying to read in a directory and get the filename from that directory.
I then want to save the filename as a variable and echo that filename out.
Here is the code I am using:
for /F %%a in ('dir C:\Users\username\Documents\Training\Pentaho\Outputs\BatchFileOutput\ *.csv') do set FileName=%%a
echo %FileName%
I am getting the following when command prompt runs:
"File not found
Directory"
Does anyone know how to resolve this or where I'm going wrong?
Thanks
Safer way of doing the same:
#echo off
setlocal
set "yourDir=C:\Users\username\Documents\Training\Pentaho\Outputs\BatchFileOutput\"
set "yourExt=*.csv"
pushd %yourDir%
for %%a in (%yourExt%) do echo %%a
popd
endlocal
Sets both: Your directory and the extension you are searching for, Changes the directory to the one previously setted possibly including a /drive change and then runs a loop over all files matching your extension and echo them out. To save only the last one you can use:
...do set fileName=%%a
echo %FileName%
Or to use them all within the loop you can use:
#echo off
Setlocal EnableDelayedExpansion
REM Other things done here
do (
REM Do stuff with %%a here
Set filename=%%a
echo !filename!
echo !filename:~0,6!
echo !filename:a=b!
)
If you just want to echo them, you can just go for echo %%a. If you want to do other things like string-substitution or substrings as described in the comments you need DelayedExpansion as shown above. There are a lot of questions on SO as well.
Note that you can get different "parts" of the path of your file. Have a look on this answer I always have a look on as well. Alternatively check the documentation for the for command typing for /? into the command-line.
I believe a windows update broke my .bat file.
What this .bat file is supposed to do is update files within the folder with a date and time using nircmd.exe.
All file names start with mmddyy for reference.
Here is the .bat file code.
REM #echo on
REM setlocal enabledelayedexpansion
FOR /R C:\Users\USERNAME\Desktop\optimize %%F in (*.*) DO call :Setfiletime %%F
goto End
:Setfiletime
SET FNAME=%~n1
echo %FNAME%
SET MM=%FNAME:~0,2%
SET DD=%FNAME:~2,2%
SET YY=%FNAME:~4,2%
nircmd.exe setfiletime %1 "%DD%-%MM%-20%YY% 18:00:00" "%DD%-%MM%-20%YY%18:00:00"
:goto :eof
:End
REM endlocal
The output now shows each line before :Setfiletime, then ends with REM.
Why is this batch file no longer working and what would I need to do to fix it?
EDIT: Fixed - file was in the wrong location. The simplest solution was the best answer.
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%
Could anybody please tell me a shell command for Windows 7 which would take a file path as argument and return the size of that file - Something like:
fileSize.cmd file.txt
...which will give me 1KB.
One question in SO noted the command echo %~z1, but for this, I have to write a separate batch file and use this command in it. I was thinking of modifying my existing bat file and incorporate this command somehow. My batch file looks like this:
p4 diff //sources/j2cs/output.txt >> diff_out.txt
I have to add above command in the existing bat file to find the file size of diff_out.txt.
You don't need an extra batch file, you could move your filename into %1 with a call to a function or you can use a FOR loop.
call :getFilesize diff_out.txt
echo %fileSize%
exit /b
:getFilesize
set filesize=%~z1
exit /b
Or
for %%A in (diff_out.txt) do set fileSize=%%~zA
another variant:
#echo off
set file=c:\bookmarks.html
%1 %0 :: %file%
set len=%~z2
echo %len%
pause
or with wmic:
D:\>set wql="drive='g:' and filename='function2' and extension='txt'"
D:\>wmic path cim_datafile where %wql% get name,filesize
FileSize Name
621 g:\function2.txt
D:\>
or:
set file=G:\function2.txt
echo set len=%%~z1 >_tmp.bat
call _tmp.bat %file% && del _tmp.bat
echo %len%