How can I output a filename without extension in batch? - batch-file

I'm a real "newbe" in batch and wrote the following script:
chdir C:\Users\oRookie\Desktop\batch
dir /b *txt > out.txt
The file out.txt is created and contains out.txt .
So far so good let's try to get more advancaded. Now I want it's content to be just out´. So I need some way to delete the fileextension in the Output, but without deleting the fileextension itself. How can I do it?
Thanks in advance.

Give a try for this code and let me know if this what are looking for or not ?
#echo off
Set Location=%userprofile%\Desktop\
Set Log=out.txt
if exist %Log% del %Log%
for /f %%i in ('dir /b /a-d %Location%*.txt') do #echo %%~ni >> %Log%
start "" %Log%

No idea why you would want to do this but you can do something like this from command line:
type nul > out.txt | for /r %i in (*.txt) do #echo %~ni >> out.txt
Or in a script:
type nul > out.txt | for /r %%i in (*.txt) do #echo %%~ni >> out.txt

This should do it:
for /f "tokens=1 delims=." %%g in ('dir /b C:\Users\oRookie\Desktop\batch') do echo %%g >> out.txt
outputs a txt named out.txt containg only the text "out"

Related

How do I combine files in order

I have/found a windows batch script that will combine csv files from all sub-directories. It works great in Windows 10 but when I run the script in Windows 7, all the files are out of order. How do I force the order in which to combine the csv files?
echo #off
for /r %%i in (*.csv) do (
if not %%~nxi == output.csv (
echo %%~nxi >> output.csv
echo %%i
echo %%~nxi
type "%%i" >> output.csv
echo. >> output.csv
echo. >> output.csv
)
)
Just a little change to your code will do the job:
#echo off
FOR /F "usebackq delims=" %%i IN (`dir /s /b /O:N *.csv`) do (
if not "%%~nxi" == "output.csv" (
echo %%~nxi >> output.csv
echo %%i
echo %%~nxi
type "%%i" >> output.csv
echo. >> output.csv
echo. >> output.csv
)
)
Point is to use another command's out -- use dir to control the sorting order.
/b for clean output by dir
/O for ordering, N -- name.
check FOR /? and dir /? for more details.
PS: you may wanna use:
echo %%~dpnxi >> output.csv
instead of line 4's echo %%~nxi >> output.csv , to show full path of each file in your output.csv.
This is just a slight modification to POW's answer that should give you better performance. This technique keeps the file open for writing. When you use the append multiple times, it is opening and closing the output file. So the file pointer has to be reset every time it outputs to the file.
#echo off
(FOR /F "usebackq delims=" %%i IN (`dir /s /b /O:N *.csv`) do (
echo %%~nxi
echo %%i >con
type "%%i"
echo.
echo.
)
)>output.tmp
rename output.tmp output.csv
Here is another possible solution:
#echo off
for /R %%A IN (*.csv) do (
if not "%%~nxA"=="output.csv" (
(echo %%~fA && type "%%~fA" && echo. && echo.)>>output.csv
echo Processed: %%A
echo %%~nxA
)
)
with much less code.
Using /R option with for to loop through subfolders in %cd%.
We want to parse ALL csv files so, we specify it in parenthesis with (*.csv).
Then, we check if csv file currently processed is output.csv.
If not, then we append full path of the csv ("%%~fA") file, its content (type "...") and two newlines (echo.) to output.csv. This condition is not really required, but added to be on-topic with the question. Also, if you don't want to append full path to output.csv, but filename and extension only just replace %%~fA with %%~nxA.
After that, we echo current file processed and its filename and extension.
If file currently processed is output.csv, then we repeat the loop.
Now, output.csv has the contents of all csv files in all subdirectories.
So, now open a new cmd and type the following commands. Read the output carefully:
for /?
if /?
echo /?
type /?
Some suggestions for further reading:
https://ss64.com/nt/for_cmd.html
https://ss64.com/nt/for_r.html
https://ss64.com/nt/syntax-redirection.html
What does "&&" in this batch file?
Thanks to all of you for your help. After reading all the responses, I realized that I could get around the windows sorting problem by sorting into a second file and then reading it from there.
dir /b /s /O:N *.csv | sort > file.txt
for /f %%A IN (file.txt) do (
if not %%~nxA == output.csv (
echo %%~dpnxA
echo %%~nxA >> output.csv
type %%A >> output.csv
echo. >> output.csv
echo. >> output.csv
)
)
Again, thank you for all your help.

Variable not Echoing in For Loop

I am trying to get the file names using a batch file from a folder but it just doesn't work.
I followed guidelines from here but for some reason this isn't returning anything at all when it should!
FOR /F "tokens=*" %%G IN ('dir /b C:\Users\Desktop\UPD\*.txt') DO SET result=%%G
I also tried:
FOR /F "tokens=*" %%G IN (dir /b C:\Users\Desktop\UPD\*.txt') DO SET _result=%%~G
echo %_result% >> %~dp0Outputfile.txt
What I get is:
ECHO is on.
EDIT
Here is what I did so far now:
IF EXIST C:\Users\Nathanael\Desktop\UPD\*.txt (
echo file found >> %~dp0Outputfile.txt
chDIR C:\Users\Nathanael\Desktop\UPD\
dir *.txt /b >> %~dp0Outputfile.txt
FOR /F "tokens=*" %%G IN ('dir /b C:\Users\Nathanael\Desktop\UPD\*.txt') DO SET result=%%G
echo %result% >> %~dp0Outputfile.txt
)
The output is:
file found
NewVHD.txt
random.txt
ECHO is on.
If the for /f returns multiple files the last one will overwrite the previous in the set
The way cmd.exe parses (code blocks) requires delayed expansion when a var is set and used inside the same code block as is the case with your if exist
So either avoid the code block with reversed logic
IF NOT EXIST "C:\Users\Nathanael\Desktop\UPD\*.txt" Goto :Eof or other label
Or (always indent code blocks to better keep track) :
Setlocal EnableDelayedExpansion
IF EXIST "C:\Users\Nathanael\Desktop\UPD\*.txt" (
echo file found >> %~dp0Outputfile.txt
chDIR "C:\Users\Nathanael\Desktop\UPD\"
dir "*.txt" /b >> %~dp0Outputfile.txt
FOR /F "tokens=*" %%G IN ('dir /b C:\Users\Nathanael\Desktop\UPD\*.txt') DO SET result=%%G
echo(!result! >> %~dp0Outputfile.txt
)
It's a good habit to always enclose pathes in double quotes
to avoid echo is off messages use an other command separator than a space if the var is possibly empty (I used a ( here
FOR /F "tokens=*" %%G IN ('dir /b C:\Users\Desktop\UPD*.txt') DO SET result=%%G
Make sure that the path is correct (for example, perhaps it should be c:\Users\YourName\Desktop\UPD*.txt where YourName is the user name)?

How can i copy a name from a file with spaces in and create a new file with this name

I am after a batch file to convert two files with the same name but different extensions
ie
test file.dat
test file.txt
I want to combine these files with a seperator (I have used ~)
I want the new file to be named a new extension but keep the original name, I have managed this but the majority of the files I use have spaces in them and I can get it to work with the spaces
The code I have so far:
type *.dat > new.andy
echo. >> new.andy
echo ~ >> new.andy
type *.txt >> new.andy
for /F %%a in ('dir /b *.dat') do set FileName=%%~na
REN new.andy %FileName%.andy
So far I have got the script to work apart from to rename the file with its full name including spaces, up to now it only goes till first space ie test.andy
Test the following
type "*.dat" > new.andy
echo. >> new.andy
echo ~ >> new.andy
type "*.txt" >> new.andy
for /F %%a in ('dir /b *.dat') do set FileName=%%~na
REN new.andy %FileName%.andy
You could start with something like this:
#echo off
for /f "tokens=*" %%a IN ('dir /b *.dat') do (
type "%%a" > "%%~na.andy"
echo. >> "%%~na.andy"
echo ~ >> "%%~na.andy"
)
for /f "tokens=*" %%a IN ('dir /b *.txt') do (
type "%%a" >> "%%~na.andy"
)
This will combine all *.dat files with all *.txt files, and the name of the combined file will be .andy.
Depending on what you are after and how your files are organized, this can be improved.

Batch script to show in txt file latest file available in a directory, by date

I have this simple batch script:
#echo off
dir /o-d "K:\DIR\DIR\*.exe" > "D:\Logs\Files.txt"
which displays all the files in a txt, by date.
My question is: how can I get this to show me only the last file by date and not all files in that specific directory?
Thanks in advance.
Retrieving the last line
If you're only interested in the last file, you can do this:
#echo off
for /f "tokens=*" %%a in ('dir /o-d "K:\DIR\DIR*.exe" ^| findstr /C:"/"') do set last=%%a
echo %last%
This displays only the last filename from the output of the dir command. If you want to redirect it to a file, replace echo %last% with:
echo %last% > "D:\Logs\Files.txt"
Retrieving the first line
If you're interested in the first file, you need to slightly alter the code to this:
for /f "tokens=*" %%a in ('dir /o-d "K:\DIR\DIR*.exe" ^| findstr /C:"/"') do set first=%%a && goto Done
:Done
echo %first%
Again, if you're interested in redirecting it to a file, replace echo %first% with:
echo %first% > "D:\Logs\Files.txt"
#echo off
setlocal
for /f "delims=" %%F in ('dir /b /o-d') do (
set file=%%~nxF %%~tF
goto display
)
:display
echo %file%
Notes:
To understand for and 'decrypt' %%~nxF %%~tF, read for help (help for from command line)
goto is there just to break the loop after first (latest) file/dir.
To redirect to file, either redirect the batch itself (batch.bat >myFile) or last echo (echo %file% > myFile)
Dir matches subdirectories as well as files by default. Use
dir /a-d to match files only.
I think it is not possible using shell/batch commands.
you may write another program to pick first line of Files.txt file
to get only the file name of the latest file
#echo off
for /f "tokens=*" %%x in ('dir /b /o-d "K:\DIR\DIR\*.exe"') do (
echo %%x
exit /b 0
)

Batch file to add character

if I have a .txt that has the following:
2005050 "2/19/2005"
2005060 "3/1/2005"
2005070 "3/11/2005"
2005080 "3/21/2005"
2005090 "3/31/2005"
Is there a way for the batch file to read and always add .png in the end of a character of 7.
For example.
2005050.png "2/19/2005"
2005060.png "3/1/2005"
2005070.png "3/11/2005"
2005080.png "3/21/2005"
2005090.png "3/31/2005"
This batch file will split each line at the first space, and append .png to the string before the split. The script reads lines from infile.txt and outputs to outfile.txt.
#echo off
echo. > outfile.txt
for /f "tokens=1*" %%i in (infile.txt) do echo %%i.png %%j >> outfile.txt
Update
Or to delete the outfile.txt first....
#echo off
del /q /f outfile.txt
for /f "tokens=1*" %%i in (infile.txt) do echo %%i.png %%j >> outfile.txt
Another Update
To just add new records to outfile.txt do something like....
#echo off
for /f "tokens=1*" %%i in (infile.txt) do (
find "%%i.png %%j" outfile.txt > nul
if errorlevel 1 then (
echo %%i.png %%j >> outfile.txt
)
)
Sed for windows
sed -r "s/^(.......)(.*)/\1.png\2/" file
My Answer failed: I did try: but I couldn't get the SET to WORK within the FOR
#echo off
set str1=ooo
set str2=ppp
for /f "tokens=*" %%a in ('type testprog.txt') do (
set str=!%%a!
echo %%str:~0,7%%.png %%str:~-5,14%% >> tempprog.txt
)
move tempprog.txt testprog.txt
start testprog.txt
Maybe someone can edit a working version as I would like to see what
I did wrong...

Resources