Batch xcopy not working - windows

I have a very simply batch file that I am trying to execute. The premise of it is simple: for every user, copy a folder and all its contents into another folder. However, upon executing I am unsuccessful. I do not get any error messages and it exits out immediately. I have ran the xcopy command itself with success, so this leads me to believe I am doing something wrong with the for loop. My knowledge with command prompt is relatively limited, so please pardon my ignorance in regards to the subject. Thanks for any help!
for /f "delims=" %a in ('dir /b /ad C:\Users') do xcopy C:\Folder "C:\Users\%a\AppData\Roaming\Folder" /f /j /s /w /y

It works for me from the command line (replacing xcopy with echo xcopy). From a batch file, though, you'll need to double your % signs, so make sure you're using %%a instead of %a.

Related

CMD Script for-loop run multiple variable executable files

I'm working on a - I thought - simple script, which can install multiple programs.
So let's see, this is my directory with the mandatory files.
C:\variables.ini
C:\programs.txt
C:\update.cmd
C:\scan.cmd
C:\storage\ *.exe
Example for *.exe is "scribus-1.4.4-windows-x64.exe"
variables.ini contains
prog=C:\programs.txt
programs.txt contains
scribus-1.4.4-windows-x64.exe
scan.cmd contains
dir C:\storage /b /a-d > programs.txt
Now the problem: update.cmd
#ECHO OFF & SETLOCAL
REM Read variables
for /f %%i in (C:\variables.ini) do set %%i
cd C:\storage\
for /f %%i in %prog% do %%i /Q /S
ping localhost -n 5 > nul
del %%i
ping localhost -n 5 >nul
exit
How works the update.cmd?
I've a main .cmd from there I call multiple .cmd files which do several things. As you see, I run in the first place the scan.cmd. It scans the c:\storage\ dir and write the content into c:\programs.txt. In the variables.ini is the programs.txt file as %prog% variable embedded. Now I run update.cmd and it takes the first variable program from the programs.txt, in this case scribus, and start the install with /silent and /quiet flag (some programms use /s and others /q, but not both, maybe a possible bug, but I had no better idea). After the install it waits for 5 seconds and remove the .exe file. If there are other executables, then the script start again til the storage dir is empty.
I hope you understand me... I tried some other ways, but I failed.
So I tried it without the variables file and executed the scan command inside the update.cmd
#ECHO & SETLOCAL
cd C:\storage\
for /F %%i in ('dir C:\storage /b /a-d') do set %%i
%%i /Q /S
ping localhost -n 5 >nul
pause
There I get some errors, that scribus is no environment variable. I don't know why it should be anyone? I tried google and read many tutorials and threads about for-loops, but not one person executed a setup file from a variable.
Does anyone know a way how I can fix this?
Many thanks in advance!!
Noting that your paths are horrible: don't install things into the root of the system drive.
As is cmd.exe: PowerShell is the future and vastly more powerful. And using ping for a delay (it sends the next ping as soon as the previous received: so not a good timing mechanism).
But I think all you need to do it launch executables is
start /wait command
which will not return until command has finished.
You also seem to be trying to run multiple commands in a for loop in cmd, this is possible:
for /F %%i in ('dir C:\storage /b /a-d') do set %%i && %%i /Q /S
using && to separate the commands (it is not clear why you have set %%i: it will just list variables with the name, you're not setting a value).

Move files in subfolders to a folder and rename windows prompt

I have almost the final command line, but I need the rename part of it, to reach the goal I want I have this:
for /f “tokens=*” %a in (‘dir /b /s /a-d’) do #copy “%a” “C:\YourFolder” /y
and it works fine, but in my case I have tons of folders with only one file on each one, this file has the same name file.ext, so, is there any way to move and change the name, for example like file1.ext, file2.ext,...
Thanks!
This is not foolproof but it's likely to work, and will prompt if the small likelihood occurs that a filename exists. You'll need a batch file to make it foolproof and to improve the naming strategy.
Remove the #echo if you are happy with the result.
cmd /v:on /c for /f "delims=" %a in ('dir /b /s /a-d') do #echo copy “%a” “C:\YourFolder\%~na-!random!!random!!random!%~xa” /-y

Batch File - FOR LOOP

Let me set the stage of my issue. I have a folder (FOLDER_ABC). Within that folder, there is a folder for my application, including a unique and always changing version number (application-v1.3.4). Within that folder, there is the application (application-v1.3.4.exe) - which will also change periodically.
C:\FOLDER_ABC\application-v1.3.4\application-v1.3.4.exe
In this section below I create a directory listing of the FOLDER_ABC for any folders starting with application* and store that folder name into a file called directory.txt. I then create a perameter and store that directory into it. I'm doing it this way, versus applying the direct full path to the directory or file, since the versions will change and I don't want to hard code the batch script.
cd C:\FOLDER_ABC\
dir application* /b /ad>"C:\FOLDER_ABC\directory.txt"
set /p verdir= <C:\FOLDER_ABC\directory.txt
Here is my issue. In the section below, I'm trying to get my batch script to run the application*.exe file, and continue on with my batch file. It currently runs my application, but it hangs and doesn't continue the rest of my batch script. I'm really new to all this coding so I appreciate the help. I assume it could be something related to me not closing the FOR loop properly? How can I get it to continue on to :FINISH?
cd "C:\FOLDER_ABC\%verdir%\"
FOR /f "tokens=*" %%G IN ('dir /b *.exe') DO %%G;
:FINISH
ECHO THE END
exit
Figured it out, but didn't have enough StackOverflow credits to answer my own question. My solution is listed below. Thanks everyone. You pointed me in the right direction.
cd "C:\FOLDER_ABC\%verdir%\"
FOR %%G in (*.exe) DO START %%G
you can try:
FOR /f "delims=" %%G IN ('dir /b /a-d *.exe') DO start "" "%%~G"
I'm not sure if this is your problem, but it is possible that the ; is causing problems. You do not terminate commands with ; in batch files.
There is no need for a temporary file. Your code can be greatly simplified with FOR:
pushd c:\folder_abc
for /d %%F in (application*) do cd "%%F"
for %%F in (*.exe) do "%%F"
:FINISH
ECHO THE END
exit /b
Try using the START command:
FOR /f "tokens=*" %%G IN ('dir /b *.exe') DO START %%G;
There may be other better ways if achieving what you want, for example, if you know that the exe always has the same name as its directory, and that there will be only one such directory, you could do the following:
FOR /D %%i in (application-v*) DO START %i\%i.exe
UPDATE
From comments:
My only issue, which I just realized, is that the application folder and application name are not always identical.
In that case, you could try something like:
for /d %%i in (application-v*) do for %%j in (%%i\*.exe) do start %%j

Windows command/commands to FIND file and COPY it to certain location or directory where batch file was launched from

So, I have started with this:
copy | dir /s /b | find "myFile" C:\Destination
but the problem is that the destination is not visible in this command. It only sees the first part of the command up until C:\Destination.
Is there a way I can search for a file and copy it?
I have also tried this:
SET source = dir /s /b | find "myFile"
SET destination = %CD%
copy %file% %destination%
but it doesn't work.
At some point even trying to set a variable that points to the current directory (%CD%) doesn't work.
Thanks in advance!
PS: I'm looking for a solution that would work without installing anything new on the computer, that's why I'm thinking of batch files.
I think I could do this with VBscript but I'm not sure. If anyone thinks it's a better option please post that answer too.
After a few hours of work I have managed to find the right combination of commands in order to make this happen. Here it is for you all and I hope it helps:
SET destination=%CD%
E:
for /f "delims=" %%a in ('dir /b /s ^| find "searchedFile"') do (
cd ..
xcopy "%%a" "%destination%" /D:10-10-2011)
pause
I used the change directory command because the "directory" command returned the entire path, including the file and when trying to copy it.. it thought that the file was in the path that included its name.
For example, if i searched for "myFile.jpg" in "E:\Folder\New Folder\myFile.jpg" it thought that the location of the file was "E:\Folder\New Folder\myFile.jpg\myFile.jpg" and obviously this doesn't work.
dir/s/b|for /f %i in ('find "myFile"') do copy "%i" .\
Works very nicely for me. Anyone know how to use the same line to copy same named files to a directory with new names.
Example:
file name is: text.txt
the above command line searches many folders and copies all instances found like below:
1text.txt, 2text.txt, 3text.txt
Place the file path in quotes
copy "%file%" "%destination%"
or
SET destination = "%CD%"
How about this?
dir/s/b|for /f %i in ('find "myFile"') do copy %i .\
Guess %i should be quoted, too...
dir/s/b|for /f %i in ('find "myFile"') do copy "%i" .\

How to run *.exe /key from the .bat in loop

I have directory structure:
DIR
|-UNINSTALL.BAT
|-component_name
|-source
|-setup.exe
|-uninst.bat
|-another_component_name
|-source
|-setup.exe
|-uninst.bat
|-yet_another_component_name
|-source
|-setup.exe
|-uninst.bat
and so on...
In every directory like "component_name" I have setup.exe file which installs current component to the palette component in Delphi.
uninst.bat contains only this string:
"setup.exe" /uninstall
So I need to write UNINSTALL_ALL.bat in DIR that would run the uninst.bat in all component directories.
Thank you in advance.
you could do it with this line:
for /f %%a in ('dir /b /s uninst.bat') do call %%a
note that the '%%' is necessary for batch files. if you are testing this on the command line, only use one '%'
That is kind of akward in a batch file. Though you could probably do it with the foreach statement. I would suggest though that you have a look at Powershell which will definitely give you the power to do this simply and a whole lot more if you want it.
You want to use the "for" construct. Something like this:
for %%i in (component_name another_component_name yet_another_component_name) do %%i\uninst.bat
The double-escaping (%%) is necessary if you put the "for" loop in a batch file. If you are just typing it out in a command prompt, only use 1 %.
Also, you may be able to use a wildcard to match against the directory names, if they follow some convention. Open up a command prompt and run "for /?" to see everything it can do...I believe there is a /d option to match against directories. That would look something like:
for /D %%d in (component_*) do %%d\uninst.bat
(obviously, adjust the wildcard to match your component directories.)
This should work:
FOR /F %%a IN ('dir /b /s uninst.bat') DO START /B %%a
if you want them to wait each other, use this:
FOR /F %%a IN ('dir /b /s uninst.bat') DO START /B /WAIT %%a
The way you describe your problem, you have only one level of sub-directories and you always call the same batch, from the root. Therefore:
Uninstall_all.cmd
#echo off
for /F "delims=" %%d in ('dir /b /ad') do cd "%%d"& start /b /w ..\uninstall.bat& cd ..
Should do the trick.

Resources