Random File Picker Picks Same Value On Each Execution (batch) - batch-file

I'm trying to launch a random file with this software via a script - the only problem is that this script always selects the same random number on every launch. It's always 41 for me... Any suggestions?
#echo on
setlocal EnableDelayedExpansion
cd C:\Users\User\Documents\Downloads\Nintendo
set n=0
for %%f in (*.*) do (
set /A n+=1
set "file[!n!]=%%f"
)
set /A "rand=%random% * 100 / 32768+1"
"C:\Users\User\Downloads\fceux-2.2.2-win32\fceux.exe" "!file[%rand%]!"

That works for me:
#echo off
setlocal enabledelayedexpansion
c:
cd \windows
for %%f in (*.*) do (
set /A n+=1
set "file[!n!]=%%f"
)
rem change random seed value
for /l %%i in (0,1,%time:~-2%) do set /A tmp=!random!
set /A rand=!random! %% n-1
echo "!file[%rand%]!"

Related

I'm trying to make a batch file that moves files by random

I already have a code, but it keeps selecting the first file on the list and it's getting irritating. I have no idea what to do.
#echo off
setlocal enabledelayedexpansion
CD c:\"destination"\somefolder
set n=0
for %%f in (*.*) do (
set /a n+=1
set "file[!n!]=%%f"
)
set /a rand=(n*%random% %%4) /4
move "!file[%rand%]!" C:\destination\somefolder
pause
rand with 30bit size(value from 0 to 2^30-1 modulo n)
#echo off
setlocal EnableDelayedExpansion
pushd c:\"source"\somefolder
set "n=0"
for /f "tokens=*" %%f in ('dir /b /a-d *.*') do (
rem number files from 0 and use full filename with spaceses
set "file[!n!]=%%~ff"
set /a "n+=1"
)
popd
if %n% leq 32768 ( set /a "rand=%random%%%n%"
) else set /a "rand=((%random%<<15)+%random%)%%n%"
move "!file[%rand%]!" C:\destination\somefolder
pause
endlocal

I need a format of code that only counts odd lines

test_title.bat
:GET_DOWNLOADS
set Counter=-1
for /f "DELIMS=" %%i in ('type version.txt') do (
set /a Counter+=2
set "Line_!Counter!=%%i"
)
if exist version.txt del version.txt
exit /b
:list_files
call :GET_DOWNLOADS
For /L %%C in (1,2,%Counter%) Do (
:: removing this part makes it work fine
set line=%%C
set /a line+=1
set /a line/=2
:: alternate way doesnt work either
REM set /a line=%line% / 2
:: this part without the math part would be %%C instead of %Line%
echo %line%. !Line_%%C!
)
pause
(made an edit)
the second part isnt working for some reason
it just crashes
if i remove the line that does the math it works fine but instead display 1. 3. 5. 7.
version.txt
everything
0
minecraft
0
steam
0
obs
0
fixed test_list.bat :D
#echo off
setlocal enabledelayedexpansion
set "num=1"
set "counter=0"
for /f "DELIMS=" %%i in (version.txt) do (
set /a num+=1
if "!num!"=="2" (set /a counter+=1&set "line_!counter!=%%i"&set num=0)
)
echo.
For /L %%C in (1,1,%Counter%) Do (echo %%C. !Line_%%C!)
pause
#echo off
setlocal enabledelayedexpansion
set "num=1"
set "counter=0"
for /f "DELIMS=" %%i in (version.txt) do (
set /a num+=1
if "!num!"=="2" (set /a counter+=1&set "line_!counter!=%%i"&echo %%i&set num=0)
)
echo.
set line_1
set line_2
set line_3
pause
Would output:
everything
minecraft
steam
obs
line_1=everything
line_2=minecraft
line_3=steam

While splitting txt file through batch script Exclamations are ommitting

Iam new to batch script and here I tried to split my text file into chunks for each 1 million rows.
Chunk files are generated as I expected, but inside the output file content Iam missing the Exclamations ( ! ) and even it skipping the immediate column after Exclamation. Please help me to get data as it is in original file into chunks!
#ECHO OFF
setLocal DisableDelayedExpansion
set limit=1000000
set feed_name=test.txt
set file=%Tgt_Dir%\%feed_name%
set lineCounter=1
set filenameCounter=1
set name=
set extension=
for %%a in (%file%) do (
set "name=%%~na"
set "extension=%%~xa"
)
setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (%file%) do (
set splitFile=!name!%date:~12,2%%date:~4,2%%date:~7,2%!filenameCounter!!extension!
if !lineCounter! gtr !limit! (
set /a filenameCounter=!filenameCounter! + 1
set lineCounter=1
echo Created !splitFile!.
)
echo %%a>> %Tgt_Dir%\!splitFile!
set /a lineCounter=!lineCounter! + 1
)
endlocal
It is a Tab delimiter file.
ScreenShot
You need to toggle delayed expansion.
setlocal DisableDelayedExpansion
for /f "tokens=*" %%a in (%file%) do (
Set "line=%%a"
setlocal EnableDelayedExpansion
set splitFile=!name!%date:~12,2%%date:~4,2%%date:~7,2%!filenameCounter!!extension!
echo(!line!>> %Tgt_Dir%\!splitFile!
if !lineCounter! gtr !limit! (
ENDLOCAL
set /a filenameCounter+=1
set lineCounter=1
echo Created file
) ELSE ENDLOCAL
set /a lineCounter=lineCounter + 1
)

Batch: Array index returns blank

In this piece of code I'm trying to make a list fit the screen in WinPE by alternating into two columns. But when I put !modelsvar[%increment%]:~20! in the echo it returns only ~20. Strange because !modelsvar[%%D]:~20! works fine. I've tried many variations of ! and % but no luck. Anyone know if there's is a specific rule that I'm missing?
I have setlocal enabledelayedexpansion enabled
set modelsx=%counter%
set /a counter=0
for /l %%D in (1,2,%modelsx%) do (
set /a counter+=1
set /a increment=!counter!+1
ECHO !counter!. !modelsvar[%%D]:~20! !increment!. !modelsvar[%increment%]:~20!
set /a counter+=1
)
for /l %%D in (1,2,%modelsx%) do (
set /a counter+=1
set /a increment=counter+1
for %%X in (!increment!) do (
ECHO !counter!. !modelsvar[%%D]:~20! !increment!. !modelsvar[%%X]:~20!
)
set /a counter+=1
)
Further details at Arrays, linked lists and other data structures in cmd.exe (batch) script
You're expanding increment immediately within ECHO, that is like %increment%, so the returned value is the one before the for loop executes.
Here is another work-around:
set modelsx=%counter%
set /a counter=0
for /l %%D in (1,2,%modelsx%) do (
set /a counter+=1
set /a increment=!counter!+1
call ECHO !counter!. !modelsvar[%%D]:~20! !increment!. %%modelsvar[!increment!]:~20%%
set /a counter+=1
)

Rename with bat file from an specific number

I have a bat file that rename all of the png files to data_ files. It starts from data_1 and data_2 .... I want to start with data_140 and data_141...
How to do that?
:: Renaming files
for %%a in (*.png) do (
set /a count+=1
set "fname=%%~a"
setlocal enabledelayedexpansion
ren "!fname!" data_!count!.png
endlocal
)
I don't see the problem. SET /A count+=1 starts with count=0 as it's not defined yet. If you want your script to start at 140 just do set count=139 before you enter the loop:
:: Renaming files
set count = 139
for %%a in (*.png) do (
set /a count+=1
set "fname=%%~a"
setlocal enabledelayedexpansion
ren "!fname!" data_!count!.png
endlocal
)

Resources