variable from registry (batch files) - variables

I am making a little mod for the old game sims 1, and i thought i make a simple batch file for installing it, just to be fancy.
anyway, in the registry you can find the installpath
[HKEY_LOCAL_MACHINE\SOFTWARE\Maxis\The Sims]
"InstallPath"="C:\\Program Files\\Maxis\\The Sims"
now, i need this to be a variable in my bat file, like
set simsdir=%installpath%
how do i do this? ive googled it but it made no sense what so ever to me so, thats why i am asking here :p
Cheers

Sounds like you need to call reg.exe
The accepted answer on 771240 looks to have the syntax you'll need. I've not tested this, but it should look something like
Set Reg.Key=HKEY_LOCAL_MACHINE\SOFTWARE\Maxis\The Sims
Set Reg.Val=InstallPath
For /F "Tokens=2*" %%A In (
'Reg Query "%Reg.Key%" /v "%Reg.Val%" ^| Find /I "%Reg.Val%"' )
Do Call Set simsdir=%%B
echo %simsdir%
edit
Maybe try it with the for all on the one line, that's the only way I could get it to work. I'm a bit rusty on the 'ol batch files though
Set Reg.Key=HKEY_LOCAL_MACHINE\SOFTWARE\Maxis\The Sims
Set Reg.Val=InstallPath
For /F "Tokens=2*" %%A In ('Reg Query "%Reg.Key%" /v "%Reg.Val%" ^| Find /I "%Reg.Val%"' ) Do Call Set simsdir=%%B
echo %simsdir%

Related

Using a Batch file can you remove part of a file name from files in a folder and all sub folders?

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"

Value search inside multiple files with results save

I have a very simple task in theory which in practice is quite tedious and time consuming.
I have in excel a list of values (table names) which i need to search individually in a folder filled with files (~ 4800 files) and even if i find or don't find results i need to record either that the table name was not found or all the file names in which the search string was contained.
We are basically trying to locate all custom development reports that make use of custom tables in our database schema. The report queries are contained outside of the DB in a *.txt file.
I'm pretty sure there probably is a way to do this automatically, but my knowledge of script building is quite limited outside SQL.
Edit:
Sorry for the lack of detail - was trying to get a new perspective. I've since used some of the information to run a different type of search for my issue and found a solution which i posted below.
First, get all files that contain your string:
findstr /mic:"table name" > contains_table.txt
Then list all files and filter the previously found names:
dir /b /a-d * | findstr /vg:contains_table.txt > dont_contain_table.txt
Sorry guys. I was trying to get a new perspective on my issue so i tried not to give too many details - but i guess it backfired on me.
But from the answer and comments I realized i was having a brain fart and I managed to run a different search and i found something similar to what i was looking for and for all intents and purposes it does what is needed.
I got the answer from: StackOverflow Thread 25402636
#echo off
setlocal enableextensions disabledelayedexpansion
set "manifest_folder=C:\!Work\!Reports\EMEA\only_reports\*.txt"
set "file_list=C:\!Work\!Reports\EMEA\file_list.txt"
set "outputFile=C:\!Work\!Reports\EMEA\results_file.txt"
(for /f "usebackq delims=" %%a in ("%file_list%") do (
set "found="
for /f "delims=" %%b in ('findstr /l /m /c:"%%a" "%manifest_folder%"') do (
echo %%a is found in %%~nxb
set "found=1"
)
if not defined found (
echo %%a is not found
)
)) > "%outputFile%"

How to set multiple line command output in different variables? (Batch)

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)

Removing everything after specific character in batch

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!
)

Batch - Search text file for specified string and if found set variable to 1

I can't seem to find a answer for this, all the questions seem focused on setting whats found in the text file as the variable. I just want to see if the string I have provided exists anywhere in the text file. (on its own line, not sharing a line with another string)
An example:
%usrname%= fish
%banfile%= is just a text file with NOTHING in it.
for /f "usebackq tokens=*" %%f in ('find /c /i "%usrname%" "%banfile%"') do set banned=1
Even with nothing in the text file, it still sets %banned%=1. I want it to set the variable %banned%=1 only if it finds one or more of the string "fish" inside the text file.
I am just beginning to understand the for command, so for most people this is probably going to be very simple. Thanks for any help!
you don't need a for loop for this:
find /i "%usrname%" "%banfile%" >nul 2>&1&&set /a banned=1 || set /a banned=0
echo %banned%
To find fish on a line that has no other text, you can use this:
#echo off
set "usrname=fish"
set "banfile=c:\folder\file.txt"
set "banned="
findstr /i /r "^%usrname%$" "%banfile%" >nul && set banned=1
The /i makes it case insensitive and it will find fish on a line by itself, anywhere in the file.