Batch File List - list

i have made a batch script creating a loterry game and with users.
The userscore is saved in in DLL datas saved in C:\RTD.
I know there are already some answers about this but i got a different meaning.
Is it possible to list the filenames somewhere without extension but also with the content !
I'd like to make a score list so it could look like this :
User1 : 25 Points
User2 : 65 Points
Banaman : 81 Points
The best would be if the thing is saved in a .txt or .dat file ...

You can try this but if you want to sort it by score later on then it would be wise to use fixed fields and entries padded with spaces. Or just pad the score with leading spaces and put the score as the first thing on each line, in the file.
#echo off
del "hiscore.dat" 2>nul
pushd "C:\RTD"
for %%a in (*.dll) do (
for /f "delims=" %%b in ('type "%%a" ') do (
>>"hiscore.dat" echo %%~na - %%b points
)
)
type "hiscore.dat"
popd
This should pad the score with leading spaces which can be sorted with the sort command.
#echo off
setlocal enabledelayedexpansion
del "hiscore.dat" 2>nul
pushd "C:\RTD"
for %%a in (*.dll) do (
for /f "delims=" %%b in ('type "%%a" ') do (
set var= %%b
set var=!var:~-10!
>>"hiscore.dat" echo !var! points by %%~na
)
)
type "hiscore.dat" |sort
popd

try this:
for %%i in (c:\rtd\*.dll) do echo %%~ni>> saved.txt

Related

Problem with variables and if statement within a for loop in a batch script

been working in this batch code for a couple of days and I cannot make it work right.
To give you a perspective, this code is intended to read a file that contains some "selected" numbers which refer to other files contained in another folder. These files contain a lot of data arranged by rows and columns. The first rows are not important so the code is skipping 50 rows to save time, then I am interested in the 2nd and 7th columns so these are exported to variables (A,B) for each row. If there is a combination of values between these two variables (e.g. A>1000 & B>0.03) on the same row the code is supposed to export the file number where this happened.
Problem: the IFs that compare these columns don't work as expected, the GTR/GEQ don't seem to respect the "more than" and the code exports file numbers even when there is no such combination in the file.
I have tried two codes so far:
Code 1
#echo off
setlocal EnableDelayedExpansion
for /f "tokens=*" %%s in (selected.txt) do (
cd %~dp0\Melts\MeltNPTdis-8-%%s
for /f "skip=50 tokens=2,7 delims= " %%A in (file.txt) do (
if %%A GTR 1000 if %%B GTR 0.03 (
cd %~dp0
>>dummy.txt echo %%s
)))
Code 2, with check variables
#echo off
setlocal EnableDelayedExpansion
for /f "tokens=*" %%s in (selected.txt) do (
cd %~dp0\Melts\MeltNPTdis-8-%%s
for /f "skip=50 tokens=2,7 delims= " %%A in (file.txt) do (
set /a x=0
set /a y=0
if %%A GTR 999 (
set /a x=1
)
if %%B GTR 0.03 (
set /a y=1
)
set /a z= !x! + !y!
if !z! EQU 2 (
cd %~dp0
>>dummy.txt echo %%s
)))
Can someone light me up on this one? I have isolated the problem to the IFs and have tried to define the variables differently like %%A or %%A%% not solving the issue.
Thanks a lot!
You would need to test both sides of the fractional numbers as cmd can only work with 32bit integers. You could however get a little help from powershell to overcome this. Note this is untested as I am on my mobile device and obviously do not have the environment you have to test:
#echo off
for /f "usebackq delims=" %%s in ("selected.txt") do (
(for /f "usebackq skip=50 tokens=2,7 delims= " %%A in ("%~dp0Melts\MeltNPTdis-8-%%s\file.txt") do powershell "if (%%A -gt 0.04) {if (%%A -gt 2) {Write-host "%%s"}}"
)>"%~dp0dummy.txt"
)

List of subfolders with 2 ghost entries how can I avoid them?

With a bat file I sorted the subfolders list in a Directory but what are the 2 folders named "." 1 dot and ".." 2 dots ? actually they are not present in windows explorer and most of all how I can avoid to show them in the list ?
#echo off
setlocal EnableExtensions EnableDelayedExpansion
> ".\Utils\Check last modified Profile.txt" (
for /F "delims=" %%D in ('
dir %APPDATA%\Mozilla\Firefox\Profiles\ /A /all-D /TW /A:D /O:-DE
') do (
rem print each item:
echo %%~D %
)
)
endlocal
image here > https://imgur.com/8XyQk6q
dir %APPDATA%\Mozilla\Firefox\Profiles\ /A /all-D /TW /A:D /O:-DE ^|findstr /v /e /L /c:"."
worked for me.
The dir output is sent to findstr which allows through lines which do not (/v) end (/e) with the literal (/L) string "."
Directories cannot be created that end with .
I believe I do not need to comment further on the attributes-selection specification.
The ^ is required to have the pipe applied to the dir command, not the for.
I'll make no further comment on the attributes-selection specified.
. is the current directory. .. is it's parent directory.
Explorer hides them.
Dir C:\windows\.\.\..
I have decided to post this because the existing answers appear to be using the same crazy and incorrect options for the Dir command. The following examples use the appropriate findstr.exe method of eliminating the . and .. directory entries from your listing, sorted according to most recently modified to least recent. I have included the full path to findstr.exe to eliminate the possibility of %PATH% and/or %PATHEXT% modifications from not being able to locate it.
If you wanted the same output format, then this should work for you:
#For /F "Tokens=1*Delims=:" %%G In ('"Dir "%AppData%\Mozilla\Firefox\Profiles" /AD/O-DE 2>NUL|%SystemRoot%\System32\findstr.exe /ELNV ".""')Do #Echo(%%H
If you don't need the empty lines then, this should omit those for you:
#For /F "Delims=" %%G In ('"Dir "%AppData%\Mozilla\Firefox\Profiles" /AD/O-DE 2>NUL|%SystemRoot%\System32\findstr.exe /ELV ".""')Do #Echo %%G
And if you only needed the directory name lines, then perhaps this will satisfy you:
#For /F EOL^=^ Delims^= %%G In ('"Dir "%AppData%\Mozilla\Firefox\Profiles" /AD/O-DE 2>NUL|%SystemRoot%\System32\findstr.exe /ELV ".""')Do #Echo %%G
You can use and add this switch /B
#echo off
#for /F "delims=" %%D in ('
dir %APPDATA%\Mozilla\Firefox\Profiles\ /A /all-D /TW /A:D /O:-DE /B
') do (
echo %%~D
)

batch file regex to get specific number from filename

I have a file name
pp-sssss-iiii-12.0.111.22_31-i-P.0.16.1.1
I want from the name only this (output desired):
12.0.111.22_31
then replace . with 0 and remove '_' so I got the below
1200011102231
well I tried to start from something like this
cd %cd%
for %%F in (*.txt) do echo %%~nxF >>1.txt
but I didnt know how to continue
edit , the code :
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=C:\Users\moudiz\Desktop\new folder\tttt"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*.pbd" '
) DO (
FOR /f "tokens=4delims=-" %%d IN ("%%a") DO (
SET "modname=%%d"
SET "modname=!modname:.=0!"
SET "modname=!modname:_=!"
ECHO %%a becomes !modname!
)
)
GOTO :EOF
pause
the name of the file
P-Script-LogFiles-1.0.33.33_123-IB-P.0.16.357.1.pbd
the output 100033033123
[note: OP belatedly asked for the first token of the name to be prepended to the name generated from the original post, hence use of %%c below]
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*.txt" '
) DO (
echo File "%%a"
FOR /f "tokens=1,4delims=-" %%c IN ("%%a") DO (
SET "modname=%%d"
SET "modname=!modname:.=0!"
SET "modname=!modname:_=!"
ECHO %%a becomes %%c!modname!
)
pause
)
pause
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
Using delayedexpansion, !var! refers to the modified value of the variable and set "var=!var:string=gnirts!" substitutes "gnirts" for "string" invarand assigns the result back tovar`.
Now - quite what you want to do with this modified result, you don't reveal - but guessing at renaming,
echo ren "%%a" "!modname!"
should be usable
To prepend the first token, simply change the tokens= to 1,4 *to select the first and fourth tokens) - and change the metavariable in the for to %%c (in order that the %%d processing remains the same), then use %%c which will contain the portion of the original filename before the first -.

Batch script to find files that contains two consecutive lines

I'm trying to code a Batch script to find txt files in a folder (and recursively in subfolders) that contain these two consecutive lines (no spaces between the lines):
Code "5898"
Price "50"
I tried with this:
Findstr -m /S /C:"Code ""5898""\r\n Price ""50"" *.txt" >> output.txt
but I don't know how to manage the carriage return and the newline. If I try to find the string without using Price ""50"" it works fine while no good results if I try to look for the two lines I need.
Please do not alter this file, doing so would probably cause it not to work.
#Echo Off
SetLocal EnableDelayedExpansion
For /F %%A In ('Copy /Z "%~dpf0" Nul') Do Set "CR=%%A"
Set LF=^
Dir/B/S/A-D|Findstr/RNF:"/" /C:"Code \"5898\"!CR!*!LF!Price \"50\"">"%tmp%\$_.tmp"
>Output.txt ( For /F "UseBackTokens=1-3 Delims=:" %%A In ("%tmp%\$_.tmp") Do (
Echo("%%A:%%B"))
Del "%tmp%\$_.tmp"
To output only the filename, (odd when the search is recursive):
#Echo Off
SetLocal EnableDelayedExpansion
For /F %%A In ('Copy /Z "%~dpf0" Nul') Do Set "CR=%%A"
Set LF=^
Dir/B/S/A-D|Findstr/RNF:"/" /C:"Code \"5898\"!CR!*!LF!Price \"50\"">"%tmp%\$_.tmp"
>Output.txt ( For /F "UseBackTokens=1-3 Delims=:" %%A In ("%tmp%\$_.tmp") Do (
Echo("%%~nxB"))
Del "%tmp%\$_.tmp"
You could technically change the Tokens to 1-2 now but I'll leave it as is so it is easier to re-implement the line number check again, If %%C==1 before the echo on the second last line if ever necessary.

Trim extra pipes off end of text file in Windows

So basically I have a process that outputs a text file that is pipe delimited, looks something like this:
|abc123|1*|004|**gobbligook|001|%|2014-01-01|||||||||||||
This is just an example, and I'm not sure if the answer involves regular expressions. If it does i will put the actual line. Anyways
ISSUE
So for this example the import process that accepts this file is looking for 8 pipes, but there are 20, if it sees any more pipes after the 8 it's looking for the import process fails.
Question
Is there a process that I can use in a Windows environment to trim the trailing pipes off the end of this for the entire file?
UPDATE
Magoo supplied me with a great answer that I am working but I keep getting this error: Delimiter was unexpected at this time
Here is code:
#ECHO OFF
SETLOCAL
SET "sourcedir=C:\Users\Desktop\Pipe Delimiter Project"
SET "destdir=C:\Users\Desktop\Pipe Delimiter Project"
(
FOR /f "tokens=1-7delims=|" %%a IN ('TYPE "%sourcedir%\test.txt"') DO (
ECHO(^|%%a^|%%b^|%%c^|%%d^|%%e^|%%f^|%%g^|
)
)>%destdir%\newfile.txt
Anyone know what's wrong? I also just put in the line from the question |abc123|..| pasted in the file like 6 times...thanks!
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=."
SET "destdir=U:\destdir"
(
FOR /f "delims=" %%a IN ('TYPE "%sourcedir%\q22863616.txt"') DO (
SET "line=%%a"
ECHO(!line:~0,-12!
)
)>%destdir%\newfile.txt
GOTO :EOF
I used a file named q22863616.txt containing your data for my testing.
Produces newfile.txt
Assuming that the final 12 fields are all empty, for lack of information otherwise.
Another form, given additional information
#ECHO OFF
SETLOCAL
SET "sourcedir=."
SET "destdir=U:\destdir"
(
FOR /f "tokens=1-7delims=|" %%a IN ('TYPE "%sourcedir%\q22863616.txt"') DO (
ECHO(^|%%a^|%%b^|%%c^|%%d^|%%e^|%%f^|%%g^|
)
)>%destdir%\newfile.txt
OK - third time's a charm.
#ECHO OFF
SETLOCAL enabledelayedexpansion
SET "sourcedir=."
SET "destdir=U:\destdir"
:: remove variables starting $
FOR /F "delims==" %%a In ('set $ 2^>Nul') DO SET "%%a="
(
FOR /f "delims=" %%a IN ('TYPE "%sourcedir%\q22863616.txt"') DO (
SET "$0=%%a"
SET "$1=%%a"
FOR /l %%c IN (1,1,8) DO SET "$1=!$1:*|=!"
SET "$2=%%a"
SET "$3="
SET /a tot=0
FOR /f "delims=:" %%e IN ('set $^|findstr /o /r "$"') DO SET /a tot=%%e - !tot! - 5
CALL :show !tot!
CALL ECHO %%$2:~0,-!tot!%%
)
)>%destdir%\newfile.txt
GOTO :EOF
:show
CALL SET "$3=%%$2:~0,-%1%%"
FOR /f "tokens=1*delims==" %%y IN ('set $3') DO ECHO(%%z
GOTO :eof
This seems immune to % in the data, but chokes on ! or &. You pays your money, you takes your choice...
This should eat trailing pipes but leave 8 of them
#echo off
type "file.txt"| repl "(.*?\|{8})\|*$" "$1" >"newfile.txt"
This uses a helper batch file called repl.bat - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
Place repl.bat in the same folder as the batch file or in a folder that is on the path.