1.

Solve : A batch file do a automatic copying and renaming job with conditions like this??

Answer»

Hello, I'm NEWBIE in command prompt console and want to ask something about my issue.

I want make a batch file do this task (basic):

- Copy all *.dat file in F:\MPEGAV\ (DVD drive in my PC) to My documents.
- If target folder is empty or there isn't any dupplicate file, then OK. But if it contains files share same name with newcoming files (copy those *.dat video files from many different VCDs but their names'd be probably same 'coz that's the way VCD named files to make them playable in VCD/DVD player), then rename them to filename-currenttime.dat.

*Note: I know basically about cmd command and try them sometime (of course still newbie way) and had search Google for putting currenttime parameter into files names. Code below (modified a example that I find in google):
Code: [Select]for /f "tokens=1-5 delims=:" %%d in ("%time%") do rename "*.dat" "*-%%dh%%e'%%f''.dat"
This command just do rename job and add time following h, m, s

I try some way to write a batch file do this job but unsucessfully. Pls someone help me out and pls write code in details (for my question, don't just tell me generally and tell me find out myself , thanks)This looks fun. It should work. I haven't tested it though.

If exist F:\MPEGAV\*.dat (
for /f "tokens=1-3 delims=:" %%a in (%time%) do (
set a=%%a
set b=%%b
set C=%%c
)
For /f "tokens=1,2 delims=." %%d in ('dir /b F:\MPEGAV') do if not exist "%userprofile%\My Documents\%%d.dat" (copy F:\MPEGAV\%%d.dat "%userprofile%\My Documents") else (copy F:\MPEGAV\%%d.dat "%userprofile%\My Documents\%%d-%a%%b%%c%.dat)
)Thanks a lot, man. I haven't try at the moment but anyway thanks for answer.

BTW, I've got some question. If the DVD/VCD drive isn't F: (not get F: as drive character due different number of HDDs, etc...) can I write code above somehow it keeps copy from valid location if 1st DVD/VCD drive isn't F: (for user have got 2 DVD drives, just need to do job with their 1st DVD/VCD drive).

Is it possible in batch file? Any environment parameter or anything made that work?

* I'll try and report to get further help if anything going wrong in current code.

I just curious about karma and got that when try playing P/S I click thanks u already

C:\batch>type nah122109.bat

Code: [Select]@echo off
cd "%userprofile%\My Documents\"
dir *.dat
pause
rem del *.dat ( remove "rem" if the *.dat should be deleted )


setlocal enabledelayedexpansion
for /f "tokens=1-5 delims=:" %%d in ("%time%") do (
echo "*.dat" "*%%d%%e%%f.dat"
echo time = %time%
echo d = %%d
set a=%%d
echo a=!a!
echo e = %%e
set b=%%e
echo b=!b!
echo f = %%f
set c=%%f
echo c=!c!
)

For /f "delims=" %%i in ('dir /b E:\MPEGAV\*.dat') do copy E:\MPEGAV\%%i "%userp
rofile%\My Documents\%%i-%a%%b%%c%.dat
)

echo cd "%userprofile%\My Documents\"
cd "%userprofile%\My Documents\"
dir *.dat
Output:

C:\batch>nah122109.bat
Volume in drive C has no label.
Volume Serial Number is F4A3-D6B3

Directory of "%userprofile%\My Documents\"

12/22/2009 07:17 PM 10 test.dat-193731.78.dat
12/22/2009 07:17 PM 10 test2.dat-193731.78.dat
2 File(s) 20 bytes
0 Dir(s) 305,515,515,904 bytes free
Press any key to continue . . .

time = 19:39:27.84
d = 19
a=19
e = 39
b=39
f = 27.84
c=27.84
1 file(s) copied.
1 file(s) copied.
cd "%userprofile%\My Documents\"
Volume in drive C has no label.
Volume Serial Number is F4A3-D6B3

Directory of "%userprofile%\My Documents\"
12/22/2009 07:17 PM 10 test.dat-193927.84.dat
12/22/2009 07:17 PM 10 test2.dat-193927.84.dat
2 File(s) 20 bytes
0 Dir(s) 305,515,515,904 bytes free

Both solutions posted take a static snapshot of the time. If there is more than one duplicate name for a single DVD/VCD, the code will break when the copy is performed and the time has not changed. I suggest leaving the clock running, NEST the for statements and use the dir command as the primary for loop.

Code: [Select]@echo off
set source=F:\MPEGAV
set target=C:\Documents and Settings\%username%\My Documents

for /f %%x in ('dir /b %source%\*.dat') do (
for /f "tokens=1-4 delims=:." %%i in ("%time%") do (
if exist "%target%\%%x" (copy %source%\%%x "%target%\%%x-%%i%%j%%k%%l"
) else (
copy %source%\%%x "%target%\%%x")
)
)

Quote

I try some way to write a batch file do this job but unsucessfully. Pls someone help me out and pls write code in details (for my question, don't just tell me generally and tell me find out myself

With an attitude like that, you'll never learn anything. Would have been better to post your unsuccessful attempt. It would have shown that you at least made an attempt for a solution.
C:\batch>type winder122109.bat

Code: [Select]@echo off
set source=E:\MPEGAV
set target=C:\Documents and Settings\%username%\My Documents

cd %target%
dir *.dat
rem del *.dat ( remove "rem" if you need to remove .dat files )

for /f %%x in ('dir /b %source%\*.dat') do (
for /f "tokens=1-4 delims=:." %%i in ("%time%") do (
if exist "%target%\%%x" (copy %source%\%%x "%target%\%%x-%%i%%j%%k%%l.dat"
) else (
copy %source%\%%x "%target%\%%x-%%i%%j%%k%%l.dat")
)
)

cd %target%
dir *.dat
Output:

C:\batch>winder122109.bat

Volume in drive C has no label.
Volume Serial Number is F4A3-D6B3

Directory of %target%
12/21/2009 11:18 AM 9 me.dat-17320759.dat
12/21/2009 11:18 AM 9 me2.dat-17320759.dat
2 File(s) 18 bytes
0 Dir(s) 305,583,677,440 bytes free
1 file(s) copied.
1 file(s) copied.
Volume in drive C has no label.
Volume Serial Number is F4A3-D6B3

Directory of %target%
12/21/2009 11:18 AM 9 me.dat-17333304.dat
12/21/2009 11:18 AM 9 me2.dat-17333304.dat
2 File(s) 18 bytes
0 Dir(s) 305,583,677,440 bytes free
@BillRichardson:

I thought you were banned. In any case if you noticed an error, why did you not simply fix it? Apparently anybody can be a critic, but if you're going to re-post code at least be accurate.

I posted:
if exist "%target%\%%x" (copy %source%\%%x "%target%\%%x-%%i%%j%%k%%l"
) else (
copy %source%\%%x "%target%\%%x")
which I admit was in error.

You posted:
if exist "%target%\%%x" (copy %source%\%%x "%target%\%%x-%%i%%j%%k%%l.dat"
) else (
copy %source%\%%x "%target%\%%x-%%i%%j%%k%%l.dat")
which is also in error.

The corrected code:
Code: [Select]@echo off
set source=F:\MPEGAV
set target=C:\Documents and Settings\%username%\My Documents

for /f %%x in ('dir /b %source%\*.dat') do (
for /f "tokens=1-4 delims=:." %%i in ("%time%") do (
if exist "%target%\%%x" (copy %source%\%%x "%target%\%%nx-%%i%%j%%k%%l.%%xx"
) else (
copy %source%\%%x "%target%\%%x")
)
)

I always thought we were here to help out the OPs. Keep in mind BillRichardson these threads are not about you but about the Topic Starters. That's all I have to say...I refuse to get involved in a marathon thread with BillRichardson, et al.

Quote from: Sidewinder on December 21, 2009, 05:23:21 PM
@BillRichardson:
In any case if you noticed an error, why did you not simply fix it? Apparently anybody can be a critic, but if you're going to re-post code at least be accurate.

_____________________________________

I respect your code and judgement more than anyone on this board.

You code is much better than mine.

I learn by running your code. The output always helps me.

I thought the new file names should have a ".dat" at the end?

I might have run the code incorrectly or had my folders set up wrong?

I will erase or change the code as you suggest.

Tell me exactly what to change.

Is it wrong to show the output?

I will do whatever you suggest.

Thanks for your help several times in the past.

Quote
With an attitude like that, you'll never learn anything. Would have been better to post your unsuccessful attempt. It would have shown that you at least made an attempt for a solution.

Well, I don't know if you're helpful for anyone previously, but not me... You just come and sounds like /I'm god, u're lazy boy, keep learning!/

Because there's people like you I wrote my question with those sentences above. I don't have to be a expert in command prompt console and I don't need to be someone like that. I'm here for get some help no matter how good/bad am I (in computer experience), people'd give a hand or not, it's their choice. You don't need to stay there and say that, it's too pointless thing after all~ 4rum just make for give people a chance to get help and it doesn't REQUIRE that much conditions (in rules, I don't see that I've to post all my attempt and etc....)

Feel free ban nick or lock acc if u want.

Don't worry, everybody is entitled to their opinion and you will not be banned for expressing yours.

It's been said here and elsewhere "Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime." Apparently you just want someone to hand you a fish.

I must have been very tired last night, but post #6 (the corrected code) has an error. The corrected code (v2) is posted below. Take it for what it's worth.

Code: [Select]@echo off
set source=F:\MPEGAV
set target=C:\Documents and Settings\%username%\My Documents

for /f %%x in ('dir /b %source%\*.txt') do (
for /f "tokens=1-4 delims=:." %%i in ("%time%") do (
if exist "%target%\%%x" (copy %source%\%%x "%target%\%%~nx-%%i%%j%%k%%l%%~xx"
) else (
copy %source%\%%x "%target%\%%x")
)
)



Quote
I'm here for get some help no matter how good/bad am I (in computer experience), people'd give a hand or not, it's their choice

I'll keep that in mind the next time you post. It will save me the aggravation of reading another one of your rants. Quote
I'll keep that in mind the next time you post.
OK, keep TALKING yourself about that.

Don't start with fish and blah blah things... I'm working as a graphic artist and I can live without cmd, alright? I'm not a kid for you talk to me like that. And I don't need any ggdamn fish, just need a accurate and correct answer for my question, if it hurt your ego, then it's not my mistake. Thanks for nonetheless.

You live as a IT supporter or programmer or whatever, me too. Then don't personally attach me about fish and etc...Quote from: nah on December 22, 2009, 07:48:15 AM
OK, keep talking yourself about that.

Don't start with fish and blah blah things... I'm working as a graphic artist and I can live without cmd, alright? I'm not a kid for you talk to me like that. And I don't need any ggdamn fish, just need a accurate and correct answer for my question, if it hurt your ego, then it's not my mistake. Thanks for nonetheless.

You live as a IT supporter or programmer or whatever, me too. Then don't personally attach me about fish and etc...
Nah, that wasn't an attack. He is just explaining that people who get taught how to do this understand it for a lifetime, no longer need help, and can even help others out (that's what happened to me), but people who only want the answer, they need help for the rest of their life. Quote from: nah on December 20, 2009, 06:56:40 AM
I want make a batch file do this task.

You were given at least 3 possible solutions.

Which solution worked for you?


Discussion

No Comment Found