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

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.

Related

Loop in .bat 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 20 days ago.
Improve this question
I must rename more hundred files, they are numbered
on the left, from 01 to 100, on the right side from G3002185 to G3002285
i use this script
rename 01.pdf G3002185.pdf
rename 02.pdf G3002186.pdf
rename 03.pdf G3002187.pdf
rename 04.pdf G3002188.pdf
rename 05.pdf G3002189.pdf
rename 06.pdf G3002130.pdf
(...)
but I would like to shorten it, is there an option to loop it somehow?
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /L %%c IN (1001,1,1100) DO (
SET "oldname=%%c"
SET /a newname=3002185 - 1001 + %%c
IF %%c==1100 (SET "oldname=!oldname:~-3!") ELSE (SET "oldname=!oldname:~-2!")
ECHO REN "!oldname!.pdf" "G!newname!.pdf"
)
GOTO :EOF
Always verify against a test directory before applying to real data.
The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO REN to REN to actually rename the files.
Your example appears to be faulty. "06" should be renamed to "G3002190".
And your count is incorrect. The last new-filename should be "G3002284.pdf"

Batch compare middle portion of two file's names to do something if they are different [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am trying to isolate a version number in an ipsw filename.
The code another submitted here below works well, but it does not work for:
iPhone_5.5_P3_14.1_18A8395_Restore
I need to isolate the "14.1" without accidentally isolating the "5.5".
The information in your opening paragraph suggests that you've not found examples of searching a directory for files with a pattern/glob and isolating a section of it. IMO, this has been covered thousands of times on this site, under the batch-file tag alone. However, here's some example code to assist you.
Please remember to adjust the path, %AppData%\Apple Computer\iTunes\iPhone Software Updates, on line 2 first, if I've guessed it incorrectly.
#For /F Tokens^=1-4^ Delims^=_^ EOL^= %%G In ('Dir /B /A:-D^
"%AppData%\Apple Computer\iTunes\iPhone Software Updates\iPhone*,?_*.?.?_*_Restore.ipsw"'
) Do #For /F Tokens^=1-3^ Delims^=.^ EOL^= %%K In ("%%H") Do #Echo Token1=%%G:Token2=%%H^
:NestedToken1=%%K:NestedToken2=%%L:NestedToken3=%%M^
:Token3=%%I:Token4=%%J^
:Token4Name=%%~nJ:Token4Extension=%%~xJ
#Pause
You can also have it as a single, difficult to read, line, if you prefer:
#(For /F Tokens^=1-4^ Delims^=_^ EOL^= %%G In ('Dir /B /A:-D "%AppData%\Apple Computer\iTunes\iPhone Software Updates\iPhone*,?_*.?.?_*_Restore.ipsw"') Do #For /F Tokens^=1-3^ Delims^=.^ EOL^= %%K In ("%%H") Do #Echo Token1=%%G:Token2=%%H:NestedToken1=%%K:NestedToken2=%%L:NestedToken3=%%M:Token3=%%I:Token4=%%J:Token4Name=%%~nJ:Token4Extension=%%~xJ)&Pause

CMD i need to move certain files in certain directory [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 5 years ago.
Improve this question
well, this is kind a simple.. i have lots file with some certain srting and i wants to move to certain folders using .bat script
here is examples
files
ABCD_Afile_1111-11111111_00001.txt
ABCD_Bfile_2222-22222222_00001.txt
ABCD_Cfile_3333-33333333_00001.txt
ABCD_Dfile_4444-44444444_00001.txt
and i wants to move this files like below
folder Afolder_1111-11111111
ABCD_Afile_1111-11111111_00001.txt
Folder Bfolder_2222-22222222
ABCD_Bfile_2222-22222222_00001.txt
folder CFolder_3333-33333333
ABCD_Cfile_3333-33333333_00001.txt
folder DFolder_4444-44444444
ABCD_Dfile_4444-44444444_00001.txt
the script should find the folder by file string 1111-1111111 and move that file to its desired folder.
i have made a batch script by my self.. but its not Exactly as i want...
eg
move D:/x/fikename.txt foldernamehere
i have to put folder name mannualy that's the problem for me..
Thanks #stephan
for %%f in (X\:x\*.txt) do ( for /f "tokens=3 delims=_" %%a in ("%%~nf") do ( for /d %%x in (X:\x\*%%a) do ( ECHO move "%%~ff" "%%x\" ) ) )
This codes works
assuming all of your files follow this syntax XXXX_Yfile_0000-00000000_00000.txt:
#echo off
for %%f in (X:x\*.txt) do (
for /f "tokens=3 delims=_" %%a in ("%%~nf") do (
for /d %%x in (X:\x\*%%a) do (
ECHO move "%%~ff" "%%x\"
)
)
)
remove the ECHO if the output looks right.

Looking for a batch script that will name files in a folder starting with A and going to Z [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 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
)

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