There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
DOS/Other
Tag Cloud
access acer asus bios bsod computer crash desktop driver drivers error ethernet excel freeze gaming hard drive hardware hdmi internet laptop malware memory modem monitor motherboard netgear network printer problem ram registry repair router slow software sound toshiba trojan usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless xbox
Search
Search for:
Tech Support Guy Forums > Operating Systems > DOS/Other >
Solved: Batch File - For Loop Counting Characters within XCopy

Reply  
Thread Tools
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
06-Jun-2009, 04:44 AM #46
Quote:
Originally Posted by scrfix View Post
Code:
::%1 is the file name from the for loop
xcopy /c "c:\users\" "z:\backup\
I am going to try it with c:\users\*.*
Perhaps that will work.
Missing quote after backup\"
And you aren't using the %1 variable so the passed file name isn't even being used.

So I think that should be xcopy /c "c:\users\%~1" "z:\backup\%~1"

I don't know where the G:\Spectacular Computer Repair\ message is coming from as this section of code is searching C:\Users, so that would seem to be from another sectin of code.
If you are copying multiple files to a destination folder that doesn't exist, it will ask the File/Directory question. Just add the /I switch to avoid that.
__________________
Microsoft MVP - Windows Expert - Consumer
Of course I know all the answers ; I just don't always match the answers to the right questions

scrfix's Avatar
Computer Specs
Member with 337 posts.
 
Join Date: May 2009
Experience: Computer Repair Expert
07-Jun-2009, 01:21 AM #47
Jerry,

Sorry. I did not catch that. Those were typing mistakes on my part. Those items are there in the code. I took out my variables and put in the code to make it easier. It looks almost exactly like you have it with the exception of variables.

I am going to look at your other code and see if I can utilize something from there.

I am using the /I switch for that reason so it should not be asking me.

G:\Spectacular Computer Repair\ is the destination. It could be z:\backup\ or whatever else. I just copied and pasted the code directly from the command prompt.


Thanks,
__________________
Wayne Leiser, Spectacular Computer Repair Computer Repair, Computer Services
World Famous Gift Baskets: Gift Baskets
Resources LLC: Water Treatment Coagulation, Water Recycling

Last edited by scrfix; 07-Jun-2009 at 01:27 AM..
scrfix's Avatar
Computer Specs
Member with 337 posts.
 
Join Date: May 2009
Experience: Computer Repair Expert
08-Jun-2009, 12:19 PM #48
Jerry,

This line doesn't make any sense to me

Code:
Set _switches=/E /Exclude:C:\Scripts\exclds
I understand that is for Exclude parameter and I remember reading soemwhere that in order to utilize Exclude you have to include a text file somewhere, I believe, to make Exclude work for XCopy however this points to a directory.

Is the text file within that directory?
Does it have to be named anything specific?

Wayne
__________________
Wayne Leiser, Spectacular Computer Repair Computer Repair, Computer Services
World Famous Gift Baskets: Gift Baskets
Resources LLC: Water Treatment Coagulation, Water Recycling
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
08-Jun-2009, 06:45 PM #49
exclds is the text file, it's in the C:\Scripts folder. I just didn't give it an extension. Just wasn't in the mood I guess

You can name it anything you want. It doesn't have to have an extension, or the extension can be anything. The only purpose for an extension is to associate files with programs so you can open the file by double clicking.
Here we are specifying a file to open, and Xcopy will treat it as a text file.

You can name the file sunset.jpg and it will still work.
__________________
Microsoft MVP - Windows Expert - Consumer
Of course I know all the answers ; I just don't always match the answers to the right questions

scrfix's Avatar
Computer Specs
Member with 337 posts.
 
Join Date: May 2009
Experience: Computer Repair Expert
09-Jun-2009, 02:22 AM #50
Thank You. I will want to create that file on the fly so I will create that in the beginning of the program and then delete it when I am done or simply use it as an include from the working directory. I wonder if I can include hidden files. I will have to test that. If I include a file with hidden attributes will they will remain hidden when executed. Something to try.

Oh, I figured out why it was asking me about the file or folder on a file. In my code I needed a \%~1 after the for statement. As soon as I added it, it copied perfectly.
__________________
Wayne Leiser, Spectacular Computer Repair Computer Repair, Computer Services
World Famous Gift Baskets: Gift Baskets
Resources LLC: Water Treatment Coagulation, Water Recycling
scrfix's Avatar
Computer Specs
Member with 337 posts.
 
Join Date: May 2009
Experience: Computer Repair Expert
09-Jun-2009, 02:29 PM #51
Hey Jerry,

This question is off the subject a little bit but I am curious as to how a batch works when it comes to exit codes.

Let's say I am running a batch file and I push the CTRL+C and terminate the batch.

By default the exit codes for xcopy get set to:

Code:
Exit Code | Description 
0 | Files were copied without error.
 
1 | No files were found to copy.
 
2 | The user pressed CTRL+C to terminate xcopy.
 
4 | Initialization error occurred. There is not enough memory or disk space, or you entered an invalid drive name or invalid syntax on the command line.
 
5 | Disk write error occurred.
I also understand that I can set the exit codes to whatever I want by set %ERRORLEVEL%=6039

Is it possible for me to run code if a user pushes CTRL+C in a bat file?

If so, how would that be accomplished? Anywhere in the bat file or would I have to wrap an if statement around the bat file?

Code:
If %ERRORLEVEL% == 2 (
REM Run this code here
) Else (
REM Run the Bat file here )
or just put this code anywhere in the bat file?

Code:
If %ERRORLEVEL% == 2 (
REM Run this code here
)
or a third option, I am totally screwed up and neither of the suggestions will work because they are extreme newb suggestions and Jerry has a much more effecient way of accomplishing what we want to accomplish

I did try the Green face for getting people not to save their files in documents and settings. They ran away screaming something about Mr. McGee and the Incredible Hulk.. I don't know. I didn't quite get it ( sad face ). I guess I will just stick to the code of copying those files as well.
__________________
Wayne Leiser, Spectacular Computer Repair Computer Repair, Computer Services
World Famous Gift Baskets: Gift Baskets
Resources LLC: Water Treatment Coagulation, Water Recycling

Last edited by scrfix; 09-Jun-2009 at 02:35 PM..
scrfix's Avatar
Computer Specs
Member with 337 posts.
 
Join Date: May 2009
Experience: Computer Repair Expert
10-Jun-2009, 04:22 AM #52
I figured out it is the latter of the two options. The same place I found the exit code levels had an example at the bottom of the page on this subject:

http://technet.microsoft.com/en-us/l.../bb491035.aspx
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
10-Jun-2009, 06:58 AM #53
Quote:
Originally Posted by scrfix View Post
I also understand that I can set the exit codes to whatever I want by set %ERRORLEVEL%=6039
If you use a set statement to assign a value to ERRORLEVEL, you can't use IF %ERRORLEVEL%==X to check the errorlevel after other commands. The Set statement will create a variable named ERRORLEVEL and will not use the variable to hold the actual errorlevel after a command. You'd have to use the If ERRORLEVEL X format.

You can use EXIT to assign an errorlevel when exiting a batch file or subroutine:
Code:
C:\>exit /?
Quits the CMD.EXE program (command interpreter) or the current batch
script.

EXIT [/b] [exitCode]

  /B          specifies to exit the current batch script instead of
              CMD.EXE.  If executed from outside a batch script, it
              will quit CMD.EXE

  exitCode    specifies a numeric number.  if /B is specified, sets
              ERRORLEVEL that number.  If quitting CMD.EXE, sets the process
              exit code with that number.
__________________
Microsoft MVP - Windows Expert - Consumer
Of course I know all the answers ; I just don't always match the answers to the right questions

scrfix's Avatar
Computer Specs
Member with 337 posts.
 
Join Date: May 2009
Experience: Computer Repair Expert
10-Jun-2009, 12:39 PM #54
I was just breaking this down and decided to test something. If I utilize one of the other compilers, it creates the bat file directly into the temp directory.

Code:
PushD %temp%
:: Delete all subfolders in temp except the one containing the batch file, if it exists.
For /R "%temp%" %%I In (.) Do If /I Not "%temp%\."=="%%I" If /I Not "%%~sI\"=="%~dps0" RD /Q /S "%%I"
:: Check if batch file is in the temp folder. If not, delete all files
If Not %~dps0==%temp% Del /F /Q *.*&Goto _tempcleared
:: batch and included files are in the temp folder
:: If a filelist wasn't included, create one to exclude the batch file.
If not exist "%~n0Filelist.txt" (
>"%~n0Filelist.txt" Echo %~nx0
>>"%~n0Filelist.txt" Echo %~n0Filelist.txt
)
For /F "Delims=" %%I In ('dir /A-D /B ^|findstr /I /V /G:"%~n0Filelist.txt"') Do Del "%%I"
:_tempcleared
PopD
:: Check if batch file is in the temp folder. If not, delete all files
If Not %~dps0==%temp% Del /F /Q *.*&Goto _tempcleared

That line is supposed to prevent that, correct?
Does it need to be:
If Not %~dps0=="%temp%" Del /F /Q *.*&Goto _tempcleared

If I copy and paste the bat into the %temp% directory and run it, the batch still gets removed. I am not sure that line is working.

With this other compiler, the bat gets renamed to something such as ~b25.bat and stored directly into the %temp% directory.

What I did to test, was to rename my bat file as that, mark it as hidden and run it directly from the %temp% directory. It was still removed. I didn't actually run it from the exe. I merely recreated the steps as to what the compiler would do.

The weird thing here is when I ran it the first time directly from the %temp% folder it removed it. I attempted it a second time and it did not remove it? huh? The only change I made was a 1 liner that said
echo I am in %temp%
pause

I put that directly after pushd so that I could troubleshoot it down. I was expecting a crash afterwards but it did not. Now every subsequent time it doesn't remove it. Weird. I will try to repeat it again but it appears to be working great now. I even removed the lines above and it is working flawlessly. I am not sure why the first time ran it removed the .bat from the temp directory.

I am able to repeat this when I use a different bat file that is not hidden. When I utilize the ~a.bat hidden it works perfectly however when I utilize the ~a2.bat that is not hidden the bat file is removed. It doesn't matter if it is in there by itself or not. Both bat files are exactly the same code.

Okay Figured Out
The line does not appear to be working unless the bat file is hidden. If the bat file is marked as hidden, this works perfectly. I attempted to perform a rmdir /S /Q %temp% directly in command line and it removes the hidden bat file. If the bat file however is not hidden, it gets removed and the program then crashes.

Any ideas?
__________________
Wayne Leiser, Spectacular Computer Repair Computer Repair, Computer Services
World Famous Gift Baskets: Gift Baskets
Resources LLC: Water Treatment Coagulation, Water Recycling

Last edited by scrfix; 10-Jun-2009 at 01:03 PM..
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
11-Jun-2009, 02:55 AM #55
Turned on echo and found the culprit:
%~dps0 = C:\DOCUME~1\THEOUT~1\LOCALS~1\temp\
%temp%= C:\DOCUME~1\THEOUT~1\LOCALS~1\Temp

Forgot the /I switch on the If statement to make it case insensitive, and the batch file path (Using %0) has a trailing backslash, whereas %temp% doesn't.

So this line:
If Not %~dps0==%temp% Del /F /Q *.*&Goto _tempcleared
Should be this:
If /I Not %~dps0==%temp%\ @Echo Del /F /Q *.*&Goto _tempcleared
I thought I tested with the batch file in the %temp% folder, but either I didn't, or those pieces got left out somehow.
Quote:
Originally Posted by scrfix View Post
Okay Figured Out
The line does not appear to be working unless the bat file is hidden. If the bat file is marked as hidden, this works perfectly. I attempted to perform a rmdir /S /Q %temp% directly in command line and it removes the hidden bat file. If the bat file however is not hidden, it gets removed and the program then crashes.

Any ideas?
The Del command won't remove Hidden files unless you use the /AH switch, but RmDir does.

Add the /I and the trailing backslash, and that should get it working.

Jerry
__________________
Microsoft MVP - Windows Expert - Consumer
Of course I know all the answers ; I just don't always match the answers to the right questions


Last edited by TheOutcaste; 12-Jun-2009 at 12:37 AM.. Reason: typo
scrfix's Avatar
Computer Specs
Member with 337 posts.
 
Join Date: May 2009
Experience: Computer Repair Expert
25-Jun-2009, 12:24 AM #56
I figured it out what I was asking. I will test and get back to you here if I have further issues.

I have switched everything over to robocopy. I still have yet to test on a XP box however Vista is working good except copying those loose files in the specified directories and the command after the for statement does not work. It skips right past it as if it didn't exist and doesn't copy loose files.

I think I have this worked out however.

Update 06-24-09
Yes, I had a stupid / at the end of the directory name I was attempting to copy to and it was screwing everything up. I added a folder after that and everything is working perfectly. I can now copy indicidual files and have the ability to edit certain files out which is what I was looking for.

Thanks to you and Squashman, I can now successfully troubleshoot these errors as well. Thank you very much for all of your assistance. It may take me much longer than you guys to troubleshoot however I feel I have learned a lot. I refer back to your explanations in these forums every time I am working with this batch file.

Hopefully, when I feel that I am at least closer to both of your levels of expertise, I can help other people as you have helped me.
__________________
Wayne Leiser, Spectacular Computer Repair Computer Repair, Computer Services
World Famous Gift Baskets: Gift Baskets
Resources LLC: Water Treatment Coagulation, Water Recycling

Last edited by scrfix; 25-Jun-2009 at 12:39 AM..
scrfix's Avatar
Computer Specs
Member with 337 posts.
 
Join Date: May 2009
Experience: Computer Repair Expert
25-Jun-2009, 04:02 AM #57
Update 06-25-09
I saw where the problem was at. The source still thought that it was in the %CD%.

I did a PushD %systemdrive%\ and then ran the code with the proper switches and that works great. It copies all of the loose files in the root directory.

If there is a more effecient way of accomplishing getting to the root directory I am all ears, well eyes, or if there is any special rules I need to follow when utilizing PushD. I also used PopD at the end of it. I am not sure if that is necessary or not however.
__________________
Wayne Leiser, Spectacular Computer Repair Computer Repair, Computer Services
World Famous Gift Baskets: Gift Baskets
Resources LLC: Water Treatment Coagulation, Water Recycling

Last edited by scrfix; 25-Jun-2009 at 04:44 AM..
Squashman's Avatar
Trusted Advisor with 18,706 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
25-Jun-2009, 12:13 PM #58
What do your mean by "ROOT"?
If you mean where the batch file exists then you can do this.
cd %~dp0

If you mean the root of your hard drive
cd \
scrfix's Avatar
Computer Specs
Member with 337 posts.
 
Join Date: May 2009
Experience: Computer Repair Expert
25-Jun-2009, 12:17 PM #59
The root of the %systemdrive%, ie C or D or whatever the systemdrive happens to be on that particular computer.

I will try cd \

Thanks and thanks again for telling me about the compilers. After figuring out those weird effects it was having, they are working out great.

I am testing the code my XP box now with Robocopy.
Okay just tested with xp inside the compiler. It will read the compiled exe however has massive amounts of errors. Time to start troubleshooting. Here goes the next day... lol
__________________
Wayne Leiser, Spectacular Computer Repair Computer Repair, Computer Services
World Famous Gift Baskets: Gift Baskets
Resources LLC: Water Treatment Coagulation, Water Recycling
Squashman's Avatar
Trusted Advisor with 18,706 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
25-Jun-2009, 12:30 PM #60
Quote:
Originally Posted by scrfix View Post
The root of the %systemdrive%, ie C or D or whatever the systemdrive happens to be on that particular computer.

I will try cd \
cd \ takes you back to the root of the current drive you are in.
The environmental variable %systemdrive% will take you to whatever that variable is set to. If your OS is installed to C: then it will take you to the root of the C: drive.

C:\>set S
SystemDrive=C:
SystemRoot=C:\WINDOWS

E:\Sea\TEST>pushd %systemdrive%

C:\>
Reply

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 01:08 PM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.