Batch: Read out number, iterate and write back - file

I want a text file with just a number that is getting iterated every time i use the batch file. I tried this so far but its not working...
set laufende_nr=0
if not exist echo %laufende_nr% > %def_log_dir%\laufende_nr.tmp
for /L %%i in (%def_log_dir%\laufende_nr.tmp) set laufende_nr=%%i
echo %laufende_nr%
set /a laufende_nr+=laufende_nr
echo %laufende_nr_neu% > %def_log_dir%\laufende_nr.tmp

Here's some quick example code for you to study:
Set "laufende_nr="
If Exist "%def_log_dir%\laufende_nr.tmp" (
Set/P "laufende_nr="<"%def_log_dir%\laufende_nr.tmp"
)
If Not Defined laufende_nr Set "laufende_nr=0"
Echo %laufende_nr%
Set/A laufende_nr+=1
(Echo %laufende_nr%)>"%def_log_dir%\laufende_nr.tmp"
Enter Set/?, If/? & Echo/? as necessary for respective information.
EditIf you have a relative directory named Set which contains an executable file named either A or P, then you would be best advised to change Set/A to Set /A and/or change Set/P to Set /P as necessary.

Related

Batch object variable use in net use command not working (System Error 67)

What I'm trying to make is a simple script to choose a network drive to be mounted then unmounted from a list. The information is storred in a CSV file.
The script first reads the CSV file and stores the parameters in an array of objects.
When I try to use the parameters in a net use command, I get a System Error 67.
Here is my code :
#echo off
setlocal EnableDelayedExpansion
set i=0
for /f "usebackq skip=1 tokens=1-4 delims=;" %%a in ("data.csv") do (
set list[!i!].name=%%a
set list[!i!].endpoint=%%b
set list[!i!].user=%%c
set list[!i!].pwd=%%d
set /a i=!i!+1
)
set "x = 0"
:SymLoop
if defined list[%x%] (
call echo %list[%x%]%%
set /a "x+=1"
goto :SymLoop
)
set /a "x+=1"
for /l %%i in (0 1 %x%) do (
call echo %%i %%list[%%i].name%%
)
set /p tomount=Which network drive would you like to mount ?
net use z: %%list[%tomount%].endpoint%% /user:%%list[%tomount%].user%% %%list[%tomount%].pwd%%
set /p dummy=Hit ENTER to unmount and exit...
net use z: /delete
pause
The CSV file looks like this (these are examples, I use correct data in my scenario):
Nom;Endpoint;ID;Password
test1;\\1.1.1.1\dir1\dir2;admin;pssword
test2;\\2.2.2.2\dir1\dir444;admin;testpwd
When I call echo %%list[%tomount%].endpoint%% /user:%%list[%tomount%].user%% %%list[%tomount%].pwd%%, I get the correct variables, and when I manually enter the net use command with the exact same parameters, it works too.
Do you have any idea how to solve this ?
Thanks.
I simply forgot to use call before my net use command !
Works now.

Batch file set to pick a random file from preset categories is not picking a random file (keeps picking the last file!)

Good evening. I enjoy modding a game in my free time, it's one those games where individual mods are packed into zip files and loaded in by simply placing said zips into a certain folder. I have hundreds of these zips and was hoping to make a batch that would pick out a random mod for me each time I use it instead of having to pick through them all myself, while placing all the unused ones into a disabled folder that I made to keep everything not being used all in one place. Every mod was prefixed with a name referring to what category it'd belong to, for example 'weapon_shotgun_GiantAngryGun9000.zip' or 'character_huck_Clown.zip'
How this batch script would work is that first it'd take every file with the specified prefixes currently in the mod folder and put them all into the disabled folder. It would then go through every single file in the disabled folder that has all the specified prefixes, pick a random file from each category, and move them into the mod folder. Currently I've gotten my batch to do all that is explained, except rather than pick a random of each, it keeps picking the alphabetically last one from each prefix list. I believe it's because the Num var being used to designate the index for the entries in the File array is not being modified or read, and in-spite my best efforts I cannot get it to work the way I'm hoping.
Any advice or solutions is appreciated, thank you for reading.
setlocal EnableDelayedExpansion
SET modFolder="C:\Game\Mods\"
SET disabledFolder="C:\Game\Mods\Disabled\"
SET itemListIndex=0
::Prefix names
SET Prefix1=Weapon_Shotgun_
set itemList[%itemListIndex%]=%Prefix1%
set /A itemListIndex+=1
SET Prefix2=Character_ThisPerson_
set itemList[%itemListIndex%]=%Prefix2%
set /A itemListIndex+=1
SET Prefix3=Weapon_Rifle_
set itemList[%itemListIndex%]=%Prefix3%
set /A itemListIndex+=1
for /l %%G in (0 1 %itemListIndex%) do (
if not "!itemList[%%G]!" == "" (
set num=0
for %%f in (!itemList[%%G]!*.zip) do (
set /A num+=1
set file[%num%]=%%f
)
set /A pick = !random! * %num% / 32768 + 1
echo Moving "!file[%pick%]!" to the mod folder
move "%disabledFolder%!file[%pick%]!" %modFolder%
)
)
set file[%num%]=%%f is echoed as set file[]='[name of file]' instead of the expected set file['(index number)']='[name of file]'
the written echo says Moving !file[]! to the mod folder instead of the expected Moving (name of file) to the mod folder
The pick variable echos as blank, implying num or random are not valid
#ECHO OFF
setlocal EnableDelayedExpansion
SET modFolder="C:\Game\Mods\"
SET disabledFolder="C:\Game\Mods\Disabled\"
SET itemListIndex=0
::Prefix names
SET Prefix1=Weapon_Shotgun_
set itemList[%itemListIndex%]=%Prefix1%
set /A itemListIndex+=1
SET Prefix2=Character_ThisPerson_
set itemList[%itemListIndex%]=%Prefix2%
set /A itemListIndex+=1
SET Prefix3=Weapon_Rifle_
set itemList[%itemListIndex%]=%Prefix3%
set /A itemListIndex+=1
for /l %%G in (0 1 %itemListIndex%) do (
if not "!itemList[%%G]!" == "" (
set num=0
for %%f in (!itemList[%%G]!*.zip) do (
set /A num+=1
set file[!num!]=%%f
)
set /A pick = !random! * !num! / 32768 + 1
CALL SET "PICKFILE=%%file[!pick!]%%"
REM echo Moving "!file[%pick%]!" to the mod folder
REM ECHO move "%disabledFolder%!file[%pick%]!" %modFolder%
echo Moving "!pickfile!" to the mod folder
ECHO move "%disabledFolder%!pickfile!" %modFolder%
)
)
GOTO :EOF
Your issue is that %num% and %pick% are the values of num and ``pick` when the outer loop (%%G) was parsed, that is, nothing.
The classic method is to use !num! for the run-time value, but you are using indirection, so you'd be tempted to use !file[!pick!]! which is ambiguous and resolved to pick. The call uses a subshell to resolve the ambiguity.
Move command disarmed by being echoed.
Despite having already received and accepted an answer, I have decided to post this to show you how I would do it.
#Echo Off
SetLocal EnableDelayedExpansion
Set "modFolder=C:\Game\Mods"
Set "disabledFolder=C:\Game\Mods\Disabled"
Rem Undefine existing variables
For /F "Delims==" %%A In ('Set item 2^>NUL') Do Set "%%A="
Rem Define filename prefixes
Set "itemListIndex=0"
For %%A In (
Weapon_Shotgun
Character_ThisPerson
Weapon_Rifle
) Do (Set /A itemListIndex+=1
Set "itemList[!itemListIndex!]=%%A_")
Rem Randomize itemListIndex
Set /A itemListIndex=(%RANDOM% %% itemListIndex)+1
Rem Define files based upon itemList[%itemListIndex%]
Set "itemFileIndex=0"
For %%A In ("!itemList[%itemListIndex%]!*.zip") Do (Set /A itemFileIndex+=1
Set "itemFile[!itemFileIndex!]=%%A")
Rem Randomize itemFileIndex
Set /A itemFileIndex=(%RANDOM% %% itemFileIndex)+1
Rem Move itemFile[%itemFileIndex%] to modFolder.
Echo Moving "!itemFile[%itemFileIndex%]!" to the mod folder
Move /Y "%disabledFolder%\!itemFile[%itemFileIndex%]!" "%modFolder%"
Pause
This way, you can easily append to the parenthesized list of prefixes, (which both significantly reduces your code and makes it simpler to maintain). It also makes more sense to randomize itemListIndex first, to reduce the number of returned .zip files for selection/moving.

How can I isolate a piece of a variable in a batch file, then save it as another variable?

Is there a way I can use a set command to isolate a few words from a variable given and then save that as another variable to use later?
If I say cd C:\users, can I take something like the C:\users and save that as a variable?
:inputstage
set /p input=Enter command:
if /i {%input%}=={get} (goto :get)
:get
set %input%==%getfile%
echo %getfile% >>Files to get.txt
call getfiles.bat
goto: inputstage
I input the following when it asks me to enter a command: "get index.html" and it then will take index.html and save it to a file called "files to get.txt" then it'll call up a piece of code that will run the command ftp -s:ftpreceive.bat that ftp receive file will then go and read the "files to get.txt" and see index.html and place that into the following ftp code get index.html(or whatever file I specify)
Can I get only a part of the input variable to become the variable getfile?
you seem to try to separate words from input or separate into <command> and <whatever>. In both cases, for is the way to go:
#echo off
setlocal enabledelayedexpansion
set "input=get something special"
echo method 1 (get "first word" and "rest"):
for /f "tokens=1*" %%a in ('echo %input%') do (
set "command=%%a"
set "param=%%b"
)
echo %command% : %param%
echo/
echo method2 (get every word):
set i=0
for %%a in (%input%) do (
set "_var[!i!]=%%a"
set /a i+=1
)
set _var

Batch file commands - Findstr, splitting and file name variables

I'm kind of new to batch commands and have been trying to automate something we do manually. I have a log file that is downloaded every day, then I search for certain items in it using Findstr (output to another file) and then split what was found into smaller files.
I'm having trouble with making file names and file name variables and using it throughout my code. The splitter code was given to me, so I'm just trying to incorporate Findstr into it. Any tips or see what I'm doing wrong?
Example:
Campaign ID: 1234
Campaign Name: Pepsi
Impression Filename: 10-06-16_file.log
Day of week: 2
It will look through 10-06-16_file.log for any rows with Campaign ID 1234 and output them to fnd_10-06-16_file.log.
If fnd_10-06-16_file.log filesize is greater than 177000kb then split the file into smaller files with name 2SplitFile1_Pepsi.log, 2SplitFile2_Pepsi.log, 2SplitFile3_Pepsi.log, etc.
setlocal ENABLEDELAYEDEXPANSION
#echo off
REM Ask for Campaign information to find
SET /P campaignid="Campaign ID(s): "
SET /P campaignname="Campaign Name: "
SET /P impressionfile="Impression Filename: "
SET /P dayofweek="Day of week: "
SET fnd_impressionfile=%campaignname%_%impressionfile%
SET maxbytesize=177000
SET fnd_impressionfile_sz=%%~zfnd_impressionfile
REM Find campaigns inside log file
findstr "%campaignname%" %impressionfile% > %fnd_impressionfile%
REM Split log file if greater than 177000 kb
if fnd_impressionfile_sz > maxbytesize (
REM Edit this value to change the name of the file that needs splitting. Include the extension.
SET BFN=%fnd_impressionfile%
REM Edit this value to change the number of lines per file.
SET LPF=1000000
REM Edit this value to change the name of each short file. It will be followed by a number indicating where it is in the list.
SET SFN=SplitFile
REM Do not change beyond this line.
SET SFX=%BFN:~-3%
SET /A LineNum=0
SET /A FileNum=1
For /F "delims=" %%l in (%BFN%) Do (
SET /A LineNum+=1
echo %%l >> %dayofweek%%SFN%!FileNum!_%campaignname%.%SFX%
if !LineNum! EQU !LPF! (
SET /A LineNum=0
SET /A FileNum+=1
)
)
)
endlocal
pause
First, change SET fnd_impressionfile_sz=%%~zfnd_impressionfile to FOR %%a in (%fnd_impressionfile%) DO SET fnd_impressionfile_sz=%%~za
Then, double check you maxbytesize. Your description says kb, but the batch implies byte. fnd_impressionfile_sz contains the byte value.
The other thing I see is there you're using the redirection operator > in if fnd_impressionfile_sz > maxbytesize. You should change it to GTR or GEQ. See HELP IF.

Batch File - for /F loop - read multiple Variables

I need to load different numeric values from external config.txt file and write them to %variables% in batch file. Example - config.txt file should looks as following:
====================
Setting1=1
Setting2=0
Setting3=1
====================
I need to assign first value (1) lets say into variable %1%, second value (0) into variable %2% and so on.
Could you please help me how to do this?
Thanks.
try this:
#echo off&setlocal
for /f %%i in (config.txt) do set "%%i" 2>nul
set "setting"
Do you know how many variables there will be? If you don't then go with Endoro's answer. If you do know how many variables there will be and you want to set them to a custom name you could do this:
#echo off
< config.txt (
set /p var1=
set /p var2=
set /p var3=
)
echo %var1%
echo %var2%
echo %var3%
pause
You shouldn't use %1% or plain numbers for variables, it can mess it up.
If config.txt had those '=' signs then you would have to skip add two more lines for the var's.
The reason you should use this for custom variables is because you could name the variables by there specific meaning which may make it easier to remember when coding.
Ex.
#echo off
< config.txt (
set /p name=
set /p pizza=
set /p car=
)
echo %name%
echo %pizza%
echo %car%
pause

Resources