Looking for a batch script that will name files in a folder starting with A and going to Z [closed] - batch-file

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have found a bunch of that rename numerically but I need one that will do alphabet. I need the first one to be renamed to just a single A and the last to a Z. I won't have more than 26 files in a folder so that shouldn't be a problem.

This loops through all the files in the directory and renames them to a single alphanumeric character. It may break over 26 files, but I haven't tested that. You also might want to add in your own file extension, but since that wasn't mentioned in the original post, I'll leave that up to you.
setlocal ENABLEDELAYEDEXPANSION
set "alpha=abcdefghijklmnopqrstuvwxyz"
set /a index=0
for /r %%i in (*) do (ren %%i !alpha:~%index%,1! & set /a index=(%index%+1)%26)

Presuming your input files are named like this
apple.txt
banana.txt
cherry.txt
Then this untested batch code (please save it in a batch file) should give you
a.txt
b.txt
c.txt
Here is the code
setlocal EnableDelayedExpansion
for %%a in (*.txt) do (
set currentFileName=%%a
REM Remove echo below to rename files
echo ren %%a !currentFileName:~0,1!%%~xa
)

Related

Windows batch merge multiple text files [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
I've been looking for a solution to my problem, but couldn't find an answer. I'm desperate.
I need to create a script, which concatenates multiple text files into one. my files in folder like this
- aaaa.txt
- aaaa (1).txt
- aaaa (2).txt
- aaaa (3).txt
- bbb.txt
- bbb (1).txt
- bbb (2).txt
- bbb (3).txt
i want merge file like this
- aaaa.txt
- bbb.txt
i have tried like this
#echo off
for /r "." %%a in (*.txt) do (
echo %%~na
type %%a >> %%~na.txt
)
but merge file is empty
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following setting for the source directory is a name
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files\t w o"
FOR %%b IN ("%sourcedir%\*(*).txt") DO (
for /f "delims=(" %%e in ("%%~nb") do (
set "destfilename=%%e"
if "!destfilename:~-1!"==" " (
type "%%b">>"%sourcedir%\!destfilename:~0,-1!.txt"
) else (
type "%%b">>"%sourcedir%\%%e.txt"
)
ECHO del "%%b"
)
)
GOTO :EOF
Always verify against a test directory before applying to real data.
Process a list of filenames matching the *(*.txt mask.
Derive the base name by re-processing the name part, tokenising on (.
Append the file found to the basename.txt; If the last character of the base name is Space, then remove that terminal space so the destination will not be "basenameSpace.txt".
The del is merely echoed, to be enabled by removing the echo if desired after testing.
Note this does not perform recursion as implied by the original code, and also processes files named basename(?).txt as well as basename(?) .txt- and the basename may not contain (

Take a part of the filename and copy it into the file [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
I'm looking for a (batch) solution to copy a part of the filename into the textfile. All of the files (thousands) are in one directory and the modified files should go to another directory (let's say c:\tmp ).
The files are called:
0001_Songtitle1.txt
0002_Songtitle2.txt
0003_Songtitle3.txt
The leading number is always 4 digits, ending with a _, whereas the Songtitle can have a different length. I want the number (the first four digits) to be copied into the first row of each file in the following format:
(for the first file) SongID:0001
(for the second file) SongID:0002
(for the third file) SongID:0003
Anybody an idea?
I tried this code:
#echo off
for %%I in (*.txt) do (
echo %%~nI>"%TEMP%\FileName.tmp"
echo/>>"%TEMP%\FileName.tmp"
copy /B "%TEMP%\FileName.tmp" + "%%~I" "%%~I.tmp">nul
del "%%~I"
ren "%%~I.tmp" "%%~nxI"
)
del "%TEMP%\FileName.tmp" 2>nul
It sends the complete filename (without .txt) into the first line. But I struggle to extract just the first four digits.
Thanks,
Ralf
Here is where I meant it to be inserted:
#echo off
for %%I in (????_*.txt) do (
for /f "tokens=1 delims=_" %%J in ("%%~nxI") do (
>"temp.tmp" echo SongID:%%J
>>"temp.tmp" echo/
copy /B "temp.tmp" + "%%~I" "%%~I.tmp">nul
move /y "%%~I.tmp" "%%~nxI" >nul
)
)
del "temp.tmp" 2>nul
I took the freedom to change your code slightly for readability and minor improvements.

How do I make a batch file create another one with code in it [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I made this batch file and then I wanted to make an installer for that with batch but then I got stuck on the making the batch file with code part.
I tried using this:
cat > %drev%:\Notekeeper\Program.bat << EOF
script script script
script script script
EOF
but then I realized I needed to add echo before every line. And that is a problem when you have 300+ lines of code...
So, is there a easier way to do it where I don't have to add echo before everything?
This is one way to do this:
#echo off
setlocal
rem Get the number of the "<resource>" line
for /F "delims=:" %%a in ('findstr /N "<resource>" "%~F0"') do set "start=%%a"
rem Skip such number of lines and show the rest of this file
(for /F "usebackq skip=%start% delims=" %%a in ("%~F0") do echo %%a) > Program.bat
goto :EOF
<resource>
script script script
script script script
There are several modifications that can be done on this code, for example:
Place the section of code at other place (not at end of this file).
More than one section of code.
Expand some %variables% when the code is being copied.
Et cetera...
Note: the inclusion of code in other types of files is managed in a section called <resource>, so I just used the same term.
#echo off & setlocal enabledelayedexpansion
(
scriptA
scriptB
)2>>fileA.txt:stream1
for /f %%p in (fileA.txt:stream1) do (
set a=%%p
set a=!a:'=!
echo !a!>> fileA.txt
)
The script above will do:
Redirect scriptA..... into fileA.txt's stream1 (you can't see that)
As fileA.txt's stream1 will be filled with:
'scriptA' is not a regonized.....
The for loop will remove the unneeded text and redirect them into
the correct fileA.txt
Here's another way, but it only shortens the >>"file.txt", not the echo
(
echo script
echo script
echo script
)>>"file.txt"
Taken from this Stack Overflow question
Edit:
As #Aacini commented, special characters will need to be escaped.

Batch File To Copy Every Line In .txt File And Then Create New .txt File For Each Line [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have .txt file contain 297 line, i want every line of that copied into new .txt file, so it will contain 297 file, for file name each file like this line1.txt line2.txt line3.txt, it possible using batch?
I have try using findrepl.bat and instruction form here batch to copy FIRST line of multiple text files but thats script for first line only.
FOR /F is what you want, with the use of SET /A to numerically increment a variable. Because CMD/batch is really only a punch card reader pretending to be a shell, the contents of source_file.txt can cause the script to break. In particular, if it has any special shell characters (like < > ! " or &), the shell will interpret those as special characters and not just echo them to the output file.
SETLOCAL ENABLEDELAYEDEXPANSION
SET LINENO=1
FOR /F "delims=" %%l IN (source_file.txt) DO (
ECHO %%l>file!LINENO!.txt
SET /A LINENO=LINENO+1
)
This should split a file and create separate files for every line.
The filenames will be the same as each line.
#echo off
for /f "delims=" %%a in (file.txt) do >>"%%a.txt" echo %%a
setlocal enableextensions disabledelayedexpansion
for /f "usebackq tokens=1,* delims=:" %%a in (
'findstr /n "^" "file.txt"'
) do echo(%%b>line%%a.txt
endlocal

how to write a basic batch process to copy files from one location to another location on windows 7 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How do I write a basic batch process to copy files from one location to another location and give a limit or rule where I can enter the number of files that can be copied and from which file to which file?
Example: If I have 300 files I want to just copy from 50 to 250 files.
#ECHO OFF
SETLOCAL
SET "destdir=c:\destdir"
SET "source=%cd%\*.bat"
for /f "tokens=1*delims=:" %%i in (
'xcopy /L /y "%source%" "%destdir%\" ^|findstr /n ":" '
) DO (
IF %%i leq 12 ECHO XCOPY "%%j" "%destdir%\"
)
should do the job, echoing the first 12 copies to be performed, copying the batch files from the current directory to the destination.
The process relies on : in the source filename, so %cd% is used in preference to .

Resources