# Solved: Need batch file help



## Lord Impaler (Oct 26, 2002)

*My Example:*

File "9872465.pdf" has been sent to the dropbox folder now the batch file is about to run.I need the batch file to move "9872465.pdf" file to folder "\\xxx\mas200\MAS 200\ASCENT\SITE\2008050069\JOBS\9872486", not sure if it matters but the file will always have the same name as the folder. This batch will need to do this to about 50 *different* files a day.

I've made a few test files and moving a specific file is quite simple, but I am not sure how to move without using an exact file name and folder location. I'm at a loss on what commands to use. Should I use robocopy? The move command doesn't seem to have enough control on what I want to do. I am not sure if the wildcard will get the job accomplished correctly either.

Need some help.


----------



## Squashman (Apr 4, 2003)

You have a mapped drive letter to this dropbox folder? What is the drive letter and path to the source directory?


----------



## Lord Impaler (Oct 26, 2002)

Yes the mapped drive is F:\

F:\FTPwos


----------



## Squashman (Apr 4, 2003)

Your description says the folder name will be the same name of the filename yet your example is not the case.


----------



## Lord Impaler (Oct 26, 2002)

Sorry for the delayed response. I just copied and pasted a quick path on the server. They should be the same.


----------



## Squashman (Apr 4, 2003)

And the folder name will already be created in your destination path or does the folder name need to be created as well based on the file name that is in your Dropbox folder?


----------



## Lord Impaler (Oct 26, 2002)

The folder is already created. The file that is being placed in the dropbox will have the same name with a .pdf or .jpg extension.


----------



## Squashman (Apr 4, 2003)

MOVE command will not accept UNC paths for the destination. So you have a few choices
1) Map a drive letter to the destination path
2) Use Xcopy to copy the file from the source to the destination and then delete the file from the source.

My biggest concern with #2 is if the file is in use (still being uploaded to your dropbox folder) and the copy fails and then deletes the file before it was copied successfully you will lose a file.


----------



## Lord Impaler (Oct 26, 2002)

Alright I mapped a drive on the serve to the other one. Just to clarify:
Target - "N:\MAS 200\ASCENT\SITE\000321101\JOBS\5072749"
Source - "D:\FTPwos\Dropbox"

For some reason the D:\ parition is also mapped as F:\.


----------



## Lord Impaler (Oct 26, 2002)

Would Robocopy be more useful for number 2? I read that Robocopy will fail to copy files that are 'locked' by other users or applications.


----------



## Squashman (Apr 4, 2003)

I don't have robocopy on my computer at work. It is not native to XP. I know it can be installed from the 2003 Server resource kit but I can't do that at work. I believe it is native on Vista and Windows 7 though.


----------



## Lord Impaler (Oct 26, 2002)

I am using this code for a batch file and I tested it in another directory to move files on my desktop to desktop folders. It works so I have come this far....


> title WOS
> @echo off
> setlocal enabledelayedexpansion
> pushd "C:\Documents and Settings\username\Desktop"
> ...


I am not sure how I can send these files to a different directory. Again I have zero experience with this and reading xcopy commands just glazed my eyes over since it gave me no instructions on how to move a specific file to a specific folder. I am also moving from one mapped drive to another.


----------



## Squashman (Apr 4, 2003)

You pretty much got the code you need. That is pretty much how I was going to do it. You are using the FOR LOOP modifiers to get just the file name without the exentions which is going to be your directory name. So now you just prefix the rest of your directory path to the File name with your move command. Replace the PUSHD command with your source directory. Change your move command to *move "%%j" "N:\MAS 200\ASCENT\SITE\000321101\JOBS\%%~nj\"*

You don't need to assign the file name without the extension to another variable. That is just slowing down your script and forces you to use delayed expansion.


----------



## Lord Impaler (Oct 26, 2002)

Squashman said:


> Change your move command to *move "%%j" "N:\MAS 200\ASCENT\SITE\000321101\JOBS\%%~nj\"*


Here is the problem. There are different site folders, and the site folders usually have mutiple job folders in them.

_move "%%j" "N:\MAS 200\ASCENT\SITE\*something here?*\JOBS\%%~nj\"_

All site numbers are unique as well as the job numbers.

Also would it be possible to have a log for this? I will put the batch on a hour schedule to move the files automatically. I was wondering if a daily log could be generated and edited with the batch, so certain employees could check to make sure the files are present and transferred.


----------



## Squashman (Apr 4, 2003)

We should be able to put the FINDSTR command inside a FOR loop to set a variable with the destination directory. Not that hard. Well at least not for me. I am sure that sounds confusing to you if you are not that familiar with this stuff.

Of course you could always replicate your folder structure in your dropbox folder and force your users to put the file in the correct folder. Then it would be a simple one line command with robocopy to sync the two folder structures.


----------



## Squashman (Apr 4, 2003)

Can use this code to find the job number. Assuming that the JOB number will only appear in one SITE. If it is in multiple sites then you are kind of SOL!


```
for /f "tokens=* delims=" %%G in ('dir /ad /b /s "N:\MAS 200\ASCENT\SITE\*%%~nj"') do set destination=%%G
```


----------



## Lord Impaler (Oct 26, 2002)

Yes, the job numbers are unique, so sites would not have the same job numbers. I appreciate your help! I'll check it out this afternoon and see what happens.


----------



## Lord Impaler (Oct 26, 2002)

Alright you lost me.... This is where I am at...

```
title WOS
@echo off
setlocal enabledelayedexpansion
pushd "D:\FTPwos\Dropbox"
for %%j in (*) do (
set name=%%~nj
if exist !name!\ (
for /f "tokens=* delims=" %%G in ('dir /ad /b /s "N:\MAS 200\ASCENT\SITE\*%%~nj"') do set destination=%%G
move "%%j" "!name!"
) else (
echo Warning: %%j not moved to !name!: folder does not exist.
)
)
popd
pause
exit
```
...but I am getting a warning saying the folder does not exist, when in fact it does.


----------



## Squashman (Apr 4, 2003)

You don't need the IF EXIST. I am off my computer for the day so I will get back to you tomorrow morning.

You can't put a FOR Loop inside and IF exist statement like that.

Nor are you using the destination variable. That is the full path to your destination directory.


----------



## Lord Impaler (Oct 26, 2002)

Alright, I just copied your code in. I do not know where to put it!


----------



## Squashman (Apr 4, 2003)

```
title WOS
@echo off

pushd "D:\FTPwos\Dropbox"
for %%j in (*) do (
	for /f "tokens=* delims=" %%G in ('dir /ad /b /s "N:\MAS 200\ASCENT\SITE\*%%~nj"') do move "%%~j" "%%~G"
)
popd
```


----------



## Lord Impaler (Oct 26, 2002)

It seems to be doing something, but I am not sure what. I run it and let it sit for about 10 minutes. I've looked at the processes and it is in fact running, but it doesn't seem to be transfering anything. I even put the batch file in the dropbox folder, but came up with the same results.

There are 2 servers the file is working with. I am putting the batch on the source server. Would this matter?


----------



## Squashman (Apr 4, 2003)

Turn the ECHO on so that you can see what it is doing.


----------



## Lord Impaler (Oct 26, 2002)

Hmmm, I turned the echo off and it moved it. I'll do multiple files to see what happens.


----------



## Lord Impaler (Oct 26, 2002)

I believe I was being impatient. It is working great, thank you Squashman!!


----------

