I want to set the command outpus (multiple lines) as variables, depending on how many lines there are. First I want to export all accessable drives on the computer to a txt file:
wmic logicaldisk get name>drivesvar.txt
Then read out these lines again and set the different drives as variables.
I tried alot witf for /f commands, but it didnt work so far. Would be glad, if someone could help me ! Thank you in advance.
you don't need a file:
#echo off
setlocal enabledelayedexpansion
set i=0
for /f "tokens=2 delims=:=" %%a in ('wmic logicaldisk get name /value ') do (
set /a i+=1
set drive[!i!]=%%a
)
set drive
If you need the colon, just add it at set drive[!i!]=%%a: (I removed it (as one of several methods) to get rid of the unusual wmic line endings)
Related
Hi I am new at using batch files and I am struggling to find a way of removing part of a file name for multiple files in a folder and all sub-folder.
the files are all named like r1_c02_200111_145423_am.csv and I need to remove the _am from the files.
I have tried the following
FOR /R "C:\Users\bob\Documents\data\" %%G IN (*_am.csv) DO REN "%%G" *.csv
but this does not change anything.
can anybody point me in the right direction please?
With the help of such a script, you can perform the required operation.
#ECHO off
FOR %%i IN (*_am.csv) DO CALL :do_rename %%i
GOTO :eof
:do_rename
SET "_file=%1"
REN "%_file%" "%_file:~0,-7%.csv"
:eof
Now more.
The search in the question was correct. To perform a rename while passing through the FOR ... DO, you can use both the SETLOCAL EnableDelayedExpansion or a procedure call. I think it's easier to use a procedure call. you don’t have to puzzle over understanding (and misunderstanding) how the SETLOCAL EnableDelayedExpansion works.
For each iteration of the loop, the Wick procedure is called with the first argument of the file name passed to it:
CALL :do_rename %%i
In the procedure itself, the argument is converted to a variable. You can already use substring operations on the variable itself. More information about substring operations can be found here.
:do_rename
SET "_file=%1"
REN "%_file%" "%_file:~0,-7%.csv"
Is there a way to set each line of a txt document as a different variable in a batch file? I have got a txt file with all drives found on the computer. How can I set each line (for each drive) as a different variable, if this is possible at all?
I have a file which writes all avalible drives in a text file. I want to read this file out and set a different variable for each line of text. For example:
DeviceID
C:
D:
E:
Now "DeviceID" would be the first variable. "C:" "D:" and "E:" are all getting individual variables. I hope you get what I mean.
Thanks in advance!
Okay so I've tried this:
#echo off
setlocal enabledelayedexpansion
set i=0
for /f "tokens=2 delims=:=" %%a in ('wmic logicaldisk get name /value ') do (
set /a i+=1
set drive[!i!]=%%a
)
set drive
pause
copy %0 %drive[1]%:\%random%.bat
pause
And it works pretty well. But I want to copy the currently executed file to all drives found without typing:
copy %0 %drive[1]%:\currentlyexecutedfile.bat
copy %0 %drive[2]%:\currentlyexecutedfile.bat
copy %0 %drive[3]%:\currentlyexecutedfile.bat
copy %0 %drive[4]%:\currentlyexecutedfile.bat
Is there a way to do this, without knowing how many drives there are and without causing errors?
Is there a way to do this, without knowing how many drives there are
and without causing errors ?
You should do it like this :
#echo off
setlocal enabledelayedexpansion
set i=0
for /f "tokens=2 delims=:=" %%a in ('wmic logicaldisk get name /value ') do (
set /a i+=1
set drive[!i!]=%%a
)
Rem set drive
Rem If you don't know how many elements the array have (that seems is the case), you may use this method:
for /F "tokens=2 delims==" %%s in ('set drive') do (echo Copy %0 "%%s:\currentlyexecutedfile.bat")
pause
SETLOCAL ENABLEDELAYEDEXPANSION
set "s=DIR D:\MyFolder /S /Q ^|FIND /i "Owner" ^|findstr /m /i "\.mkv$""
for /f "Tokens=5,6*" %%a in ('%s%') do (
SET _endbit=%%aa:*STRING=%
CALL SET _result=%%aa:%_endbit%=%%
>>%tmp%\list.txt echo %_result% %%b %%c
)
wscript "C:\my.vbs"
I am listing my files that owned by Owner and has extension mkv from MyFolder. I want to remove everything after specific character/word. I wrote that code. But It seems to be not working.
First of all, is it possible to do that? If so what is wrong with my code?
You came up with the same solution I was going to post on your other question - but I think you're missing some things. The line ending STRING=% is missing another % at the end, and the line %%aa:%_endbit%=%%, I'm not sure it can work directly on the variable from the for loop, and you need to store it in another variable first, and you probably need some expansions using ! characters too.
Here's what I had that seems to work, just as a test in a folder with three files in it, removing the end of the filename using E0 as the cutoff string:
SETLOCAL ENABLEDELAYEDEXPANSION
#echo off
for %%f in (*.mkv) do (
set FULLNAME=%%f
set ENDTEXT=!FULLNAME:*E0=!
call set TRIMMEDNAME=%%FULLNAME:!ENDTEXT!=%%
echo !TRIMMEDNAME!
)
I have a batch file that i'm having issues with. I need to find the name of a file, then set it to a variable. Then I will use this to pass it onto a vbs script to further look into the file. The name of the file is logfile_date_time.log but the time varies depending on what time is starts. The point of the batch file is to find out the last modified date of this file.
set fordate=%date:~4,2%%date:~7,2%%date:~10,4%
set filename=c:\logfile_%fordate%_*.log
if exist %filename% (goto exist) else (goto noexist)
:exist
vbsscript.vbs /file:%filename%
goto end
:noexist
file doesn't exist code blah blah
:end
pause
I had to modify the names of folders and remove some code for security purposes since this is for work.
Any help appreciated. Thanks!
not tested:
set "last_modified="
for /f "delims=" %%f in ('dir /a-d /tw /o-d /b^| findstr /r /i /c:"logfile_[0-9][0-9]*_.log"') do (
do set "last_modified=%%~dpfnxf"
goto :break_loop
)
:break_loop
if defined last_modified echo file %last_modified% exist ...
The problem with your code is that it doesn't expand the wildcard character (*), and your VBScript probably doesn't handle wildcards in filenames by itself (the FileSystemObject methods for instance don't). If the file you want to process is the only one matching your pattern, you could do something like this:
#echo off
setlocal
set "fordate=%date:~4,2%%date:~7,2%%date:~10,4%"
pushd C:\
for %%f in (logfile_%fordate%_*.log) do vbsscript.vbs /file:"%%~ff"
popd
first my batch script snippet (not working):
set _WRx32=wrar420.exe
set _WRx64=winrar-x64-420.exe
set _WRLNG=rarlng.rar
FOR %%f IN (_WRLNG, _WRx64, _WRx32) do set "%%f"="%%~nf"
[ for every variable name (!) in the list remove the extension from the variable content (!)
and save back to the same variable ]
Question: How can i remove the extension of the filenames which are saved in the
variables _WRLNG, _WRx64, _WRx32 and save this filename (without extension)
back to the same variables ??
i need something like: set var=%var~n% but that doesn't work. And in a FOR loop i don't know
how to convert the name from the (...variable names ...) list to the corresponding variable
so i can apply a parameter extension like "~n", "~nf" etc. on it.
Can anyone help me ?
Thanks
#echo off
setlocal EnableDelayedExpansion
set _WRx32=wrar420.exe
set _WRx64=winrar-x64-420.exe
set _WRLNG=rarlng.rar
FOR %%f IN (_WRLNG, _WRx64, _WRx32) do (
FOR %%g in (!%%f!) do set "%%f=%%~ng"
)
Try this:
FOR /f "delims=." %%f IN ("%_WRx32%") do set "_WRx32=%%f"
FOR /f "delims=." %%f IN ("%_WRx64%") do set "_WRx64=%%f"
FOR /f "delims=." %%f IN ("%_WRLNG%") do set "_WRLNG=%%f"
I tried a similar version on the command line, and it appears you can't do multiple variable variables in a for loop (that I know of).
So, you have two options:
If you know that the variables will always have a 4 character extension (including the period), then you could use something like set _WRLNG=%_WRLNG:~0,-4%. This will take the last 4 characters from the variable.
You have to do one loop per variable, and you need to fix up the way you are doing them. The proper way would be for /f %%i in ("%_WRLNG%") do set _WRLNG=%~ni.
Or, look at Endoro's answer for a better for loop example.
EDIT: Thought of another way:
#echo off
for /f "delims=. tokens=1,2" %%i in ("%var%") do set var=%%i