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

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 .

Related

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.

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
)

Continue name and numbering [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to add photos to existing photo folder. If my last photo in the folder is photo 56.jpg and I want to add 10 more new photos and want them to continue name and numbering like photo 57.jpg ; photo 58.jpg so on....
Can some one help with a batch file for this.
#echo off &SETLOCAL ENABLEDELAYEDEXPANSION
SET "photofolder=photos"
SET "newfolder=newphotos"
FOR %%a IN ("%photofolder%\Photo *.jpg") DO (
FOR /f "tokens=2" %%b IN ("%%~na") DO IF %%b gtr !hi! SET /a hi=%%b
)
FOR %%a IN ("%newfolder%\*.jpg") DO (
SET /a hi+=1
COPY "%%~fa" "%photofolder%\Photo !hi!%%~xa"
)

How can I copy only files and folder created in the past hour in one directory to a new 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 4 years ago.
Improve this question
Sorry, I have very little programming experience, but I'll try my best to make this make sense.
Basically, I want to create a program (a batch file possibly?) that runs every hour that deletes everything in the target directory, then copies any files or folders created in the past hour in the source directory to the target directory. Files and folders in the source directory should be unaffected. The deleting and copy functions could be two separate programs, if that makes things easier. They would just have to run in that order.
I've looked into a lot of different solutions, like Robocopy and xcopy, but I haven't been able to figure out how to make use of the /xo command in Robocopy without having anything in the target directory, and xcopy just doesn't seem to be able to accept this level of specificity.
#echo off
setlocal EnableDelayedExpansion
set target=C:\The\Target Directory
set source=C:\The\Source Directory
rem Deletes everything in the target directory
echo Y | del "%target%" > NUL
rem Copies any files or folders created in the past hour in the source directory
for /F "tokens=1,2 delims=:" %%a in ("%time%") do set /A "oneHourAgo=(1%%a-101)*60+1%%b-100"
for %%a in ("%source%\*.*") do (
for /F "tokens=2-4 delims=: " %%b in ("%%~Ta") do (
set /A "fileTime=(1%%b-100)*60+1%%c-100"
if "%%d" equ "p.m." (
if %%b neq 12 (
set /A fileTime+=12*60
) else (
set /A fileTime-=12*60
)
)
)
if !fileTime! gtr !oneHourAgo! copy "%%a" "%target%"
)
This Batch file don't works correctly when the "last hour" pass over midnight. This point may be added, if required.
Robocopy doesn't have the granularity, but XXcopy seems to handle it.
/DA#30m selects files made within the last 30 minutes.
XXcopy is free for non commercial use and can be downloaded from http://www.xxcopy.com
XXcopy is xcopy on steroids.
If the file names are predictable (which you do not say), I would write a simple logrotate script for Windows batch, like this one, and use windows task scheduler to run once per hour. This script could be adjusted to make 3, 10, , 24, or more rotations.
#echo off
setlocal
CD logdir
del the.log.3
ren the.log.2 the.log.3
ren the.log.1 the.log.2
ren the.log.0 the.log.1
ren the.log the.log.0

Resources