changing directory stops %~dp0 from working - windows

I have two batch file on the C:\ drive and am using %~dp0 command to use the path of the first script to make a copy the second batch script:
COPY %~dp0"Hello World.BAT" C:\"Hello World.bak"
Early in the script I am required to change to a sub directory off the root of the C:\ but this stops the above copy command from working the error I get is "the file cannot be found". If I stay in the root of the C:\ the copy command works perfectly. Any ideas why this is happening.

Another way to solve this would be to save %~dp0 in another variable at the beginning of your script.
#echo off
setlocal
set filepath=%~dp0
.
.
some code
.
.
cd away from original path
.
.
COPY "%filepath%Hello World.BAT" "C:\Hello World.bak"
That should work.
I am tempted to think the reason it is not working has to do with your quotes.
You have this:
COPY %~dp0"Hello World.BAT" C:\"Hello World.bak"
replace it with this:
COPY "%~dp0Hello World.BAT" "C:\Hello World.bak"
you need to wrap the entire path in quotes to be sure it will work. If you have:
C:\Program Files\Somefolder\
as your path and use the quotes how you have them it will turn out like this:
"C:\Program Files\Somefolder\""Hello World.bak"
and it won't work.

I haven't exactly worked out in my mind how changing the current directory causes the command to fail when it works before the change. But I notice that the quotes are not placed optimally. Spaces in the path would cause the command to fail, though it seems to me it should fail regardless of your current directory.
I would use:
COPY "%~dp0Hello World.BAT" "C:\Hello World.bak"
Moving the quote to the front of the 1st argument is potentially important. Moving it for the 2nd argument is not important since there are obviously no spaces in the path, but it looks better to me.
edit
After reading your question more carefully, I'm thinking there must be more to the story. If both batch files are in the root of the C drive, then your original posted code should work.
Try editing your script to diagnose what is happening. Put ECHO before the copy command so you can see what the script is attempting to do. (or simply make sure echo is on, but then it may be harder to find the correct line in the output.)
echo COPY %~dp0"Hello World.BAT" C:\"Hello World.bak"
If you still can't figure out what is wrong, post the results so others might help.

Related

bash: how to combine cd and ./?

Lets say I have a folder called xfile, inside of it we have runthis which is executable.
So, in order to execute that, I have to cd xfile and then ./runthis. and it will work just fine.
But, If I have a script call it run.sh and do this ./xfile/runthis, it will says run.sh: line xx: ./xfile/runthis: No such file or directory
So, how to make it happend without doing cd? It's been bugging me alot lately.
The leading dot in your command ./runthis means "relative to the current directory". But when you want to call it from outside the directory, remove the dot and provide the absolute path of your executable file xfile/runthis. The same is true when you want to call it from your run.sh

how to delete a directory in cmd, without deleting its content?

Im new to cmd, and I was given a task to delete a directory, without deleting its content, in one line.
How can I do that?
I had already tried rmdir and del, but they remove the content of the directory along with it.
I thought about using move first in order to move the content, but I have no idea as for how to do it along with deleting the folder in one line.
Any help would be much appreciated
Like this on all Microsoft OSes since 2000, and still good today:
dir & echo foo
If you want the second command to execute only if the first exited successfully:
dir && echo foo
As answered here.
So you can easily move, then delete the folder all in one line.

How do i use XCOPY with spaces in both paths, even with quotes?

I am trying to create a batch file that copies files from one path to another, using several xcopy commands. However, the batch script fails because Xcopy apparently has the wrong number of parameters.
I believe the reason is that it thinks the spaces in my folder paths are separating parameters- but I do not know why it is doing this because I have made sure that both the source and destination paths are surrounded with double-quotes.
for example, I run this command in my batch script:
c:/windows/system32/xcopy.exe "H:\some path with spaces\myfile.txt" "H:\some path with spaces\a_different_folder\myfile.txt" /Y
and it outputs this:
Invalid number of parameters
Can somebody please tell me what I'm doing wrong or if there is a workaround?
I have also tried using the standard copy command, but that ends up saying the system cannot find the path specified (which isn't true, as my batch file is actually generated by a script that uses the paths of files that are guaranteed to exist)
as #Mark said:
It is your path to xcopy. It is wrong. It is why you are getting the message. C: is a valid command. It will choose a valid command over fixing unix's paths

OS X bash For loop only processes one file in a directory

I'm trying to get this code to process all files in a directory : https://github.com/kieranjol/ifi-ffv1/blob/master/ifi-ffv1.sh
I run it in the terminal and add path to file ./ifi-ffv1.sh /path/to/file.mov. How can I get it to move on to the next? I'll also need to make sure that it only processes AV files, such as .avi/.mkv/*.mov etc.
I've tried using while loops with shift but I can't get that to work either.
I've tried adding a specific path like here but I'm failing http://www.cyberciti.biz/faq/unix-loop-through-files-in-a-directory/
I've tried this https://askubuntu.com/a/315338 and it keeps looping the same file rather than moving on to the next one. http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html this didn't help me either.
I know this is going to be a horribly simple solution but I'm very new to this.
You don't actually have any kind of loop in your code. You need to do something like
for file in path/to/*.avi path/to/*.avg
do
./ifi-ffv1.sh "$file"
done
which will loop through all the specified files and substitute each one for $1
You can put whatever file names you want instead of the path/to/*.avi path/to/*.avg. If you cd to the directory first, you can leave out the paths, and just use *.avi *.avg
To do it all in one script, do something like this:
cd <your directory>
for file in *.avi *.avg
do
<your existing script here>
done
replacing all the $1's in your script with "$file" (not duplicating any quotes you already have, of course)

Batch file to detect only part of a filename

I need a snippet of a batch file that can detect part of a filename, then rename it.
Note that the numbers after the filename change randomly but "file" is always the same.
Example:
filename: file143424
There will always be only one file that needs to be renamed in the folder. The script will delete older versions of the file. For instance, I put a first file inside the folder and the script in there too. I then run the script, which renames it to just file. Then if I put a new file in, the script, when run again, will recognize it from the "file" prefix and rename it to file after deleting the old one.
Excuse me. Your question is incomplete, I think. You want to rename "file134342" to what?
In your comment you said: rename it to just "file", but this works only if there is just one file with that name. Anyway, here it is:
for %%f in (file*) do ren %%f file
If this is not what you want, then give us more details (you showld always give full details from the very beginning).
You can check for the first four characters that way:
if "%filename:~0,4%"=="file" ...
#Aacini's suggestion should work fine in your case. But you could do very well without a for loop, a single REN (RENAME) command would be enough:
RENAME file* file

Resources