Need help in amending a script.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
tweacle
Posts: 71
Joined: 11 Apr 2018 05:38

Need help in amending a script.

#1 Post by tweacle » 28 May 2018 10:00

Hi

I have the following script that moves stuff from C:\Downloads\BTP to S:\BTP DOWNLOADS and also from C:\Downloads to S:\DOWNLOADS

Code: Select all

@echo off &setlocal DisableDelayedExpansion
for /f "delims=" %%i in ('dir /ad /b "C:\Downloads\BTP*"') do robocopy "C:\Downloads\%%i" "S:\BTP DOWNLOADS\%%i" /s /e /move /r:1 /w:1
for /f "delims=" %%i in ('dir /ad /b "C:\Downloads\*"') do robocopy "C:\Downloads\%%i" "S:\DOWNLOADS\%%i" /s /e /move /r:1 /w:1
Im trying to amend it to look in C:\Downloads and if subfolder name starts with the letters BTP then I need it moving to S:\BTP DOWNLOADS and if not then I need the folder moving to S:\DOWNLOADS

Any ideas.

Thanks

tweacle
Posts: 71
Joined: 11 Apr 2018 05:38

A Simple move file command help

#2 Post by tweacle » 29 May 2018 06:41

Hi

Im totally baffled after looking on the net at all the options avail.

What im trying to do is set up a .VBS command to move files that start with the name BTP in C:\Documents into S:\Documents .

Trouble is that the file will always start with the letters BTP but will then have different numbers after it.

Any Ideas.

Thanks

MarioZac
Posts: 38
Joined: 16 May 2018 00:12

Re: Need help in amending a script.

#3 Post by MarioZac » 29 May 2018 09:27

Pls add exit /b to the end of your batch, and then post errors printed in open Cmd Window, when running your current batch from that window?

Pls post examples of full file names moved with your current script, and compare them to examples of full file names that were NOT moved, but you expect the script to move.

tweacle
Posts: 71
Joined: 11 Apr 2018 05:38

Re: Need help in amending a script.

#4 Post by tweacle » 30 May 2018 11:27

Apologies but I got myself confused.

I have now created this command of which 90% of what I need it to do.

@echo off
setlocal
set source=C:\Users\g\Documents
set dest=C:\Users\g\Documents\btp
set prefix=B

for /f "tokens=*" %%a in ('dir /b "%source%\%prefix%*"') do (echo Moving "%%a"
xcopy "%source%\%%a\*.*" "%dest%\%%a\" /s /h /k /c /y
rd "%source%\%%a" /q /s)

echo Done.

It moves all the folders that start with the prefix "B" but im having to 7zip and password protect the folder BEFORE moving. It then dont recognise the folder although the name still starts with the prefix "B" . I think its because 7zip is a .EXE file . Is there anyway the script can be amended??

Thanks

MarioZac
Posts: 38
Joined: 16 May 2018 00:12

Re: Need help in amending a script.

#5 Post by MarioZac » 30 May 2018 11:52

Your task is now changed beyond recognition. :D Where is your archiving code snippet?

If %%a looks like x.7zip , and you try to copy %%a\*.* , which means x.7zip\*.* , would you expect XCOPY to look inside each 7zip archive? Try xcopy "%source%\%%a" "%dest%\%%a" - any issues?
Last edited by MarioZac on 30 May 2018 13:45, edited 1 time in total.

tweacle
Posts: 71
Joined: 11 Apr 2018 05:38

Re: Need help in amending a script.

#6 Post by tweacle » 30 May 2018 11:57

will give that a try thanks

tweacle
Posts: 71
Joined: 11 Apr 2018 05:38

Re: Need help in amending a script.

#7 Post by tweacle » 30 May 2018 13:59

Sorry I think im confusing you after trying that. Let me try and explain simpler.

I have a file that I have 7 zipped and password protected separately and thats fine.

The code shown below is to move documents to C:\Users\g\Documents\btp BEFORE the file is zipped and password protected but what im trying to do is move it AFTER it zipped and password protected.

I believe that the reason it is not moving after it zipped etc is due to it being an application .exe file rather than a folder. Im trying to find out if I can amend below so that it looks for .exe files that start with the prefix B and move them.

Code: Select all

@echo off
setlocal
set source=C:\Users\g\Documents
set dest=C:\Users\g\Documents\btp
set prefix=B

for /f "tokens=*" %%a in ('dir /b "%source%\%prefix%*"') do (echo Moving "%%a"
xcopy "%source%\%%a\*.*" "%dest%\%%a\" /s /h /k /c /y
rd "%source%\%%a" /q /s)

echo Done. 
Thanks

MarioZac
Posts: 38
Joined: 16 May 2018 00:12

Re: Need help in amending a script.

#8 Post by MarioZac » 30 May 2018 20:29

Pls READ above post #5. Try modifying your script as suggested, then report any errors printed in open Cmd window or discovered. :D


UPDATE: The error the modified code prints: Cannot perform a cyclic copy. If the code doesn't work as expected, add Exit /b at the end and debug it in an open Cmd Prompt window to see variable values and errors it prints. Did you ever try that? What Google suggests for that error if you do the search: "for Xcopy place the destination folder outside of the source folder. Or use Robocopy with /xd switch to exclude the destination folder from being recursively copied".

MarioZac
Posts: 38
Joined: 16 May 2018 00:12

Re: Need help in amending a script.

#9 Post by MarioZac » 31 May 2018 10:49

Try this script.

Code: Select all

@echo off
setlocal
set "source=O:\Temp\Other" & set "dest=O:\Temp\Other\btp" & set "prefix=B"
for /d %%B in ("%dest%") do set "fold=%%~nxB"
for /f "tokens=*" %%a in ('dir /b "%source%\%prefix%*"') do ( echo Moving "%%a"
	if exist "%source%\%%a\" ( xcopy "%source%\%%a" "%dest%\%%a" /s /h /k /c /y /i & if not %%a==%fold% rd "%source%\%%a" /q /s 
	) else ( xcopy "%source%\%%a" "%dest%\Files\%%a*" /c /y /i & del %source%\%%a ) 
)
endlocal
exit /b

tweacle
Posts: 71
Joined: 11 Apr 2018 05:38

Re: Need help in amending a script.

#10 Post by tweacle » 02 Jun 2018 05:13

Tried your last suggestion MarioZac and get error message enviorment variable C:\CCTV DOWNLOADS\BTP DOWNLOADS not defined and then it tries to moved file and finishes but dont actually move anything.

tweacle
Posts: 71
Joined: 11 Apr 2018 05:38

Re: Need help in amending a script.

#11 Post by tweacle » 02 Jun 2018 06:19

Ignore my comments about not defined as sorted that but is there anyway I can add to this script so if file dont start with prefix "B" I need it moved to "O:\Temp\Other\Other"

Code: Select all

@echo off
setlocal
set "source=O:\Temp\Other" & set "dest=O:\Temp\Other\btp" & set "prefix=B"
for /d %%B in ("%dest%") do set "fold=%%~nxB"
for /f "tokens=*" %%a in ('dir /b "%source%\%prefix%*"') do ( echo Moving "%%a"
	if exist "%source%\%%a\" ( xcopy "%source%\%%a" "%dest%\%%a" /s /h /k /c /y /i & if not %%a==%fold% rd "%source%\%%a" /q /s 
	) else ( xcopy "%source%\%%a" "%dest%\Files\%%a*" /c /y /i & del %source%\%%a ) 
)
endlocal
exit /b

MarioZac
Posts: 38
Joined: 16 May 2018 00:12

Re: Need help in amending a script.

#12 Post by MarioZac » 02 Jun 2018 09:09

There are always ways to do simple file operations in a batch. This forum is learning environment. You seems to be learning now how to check a batch code for execution errors. What is your extra code version? What errors does it give?

Hint: consider setting Dest1 and Dest2 variables, and extra FOR and/or IF enclosed commands. Also, if you copy from C:\ to O:\, the script can be simplified as source folder is different from the destination folder. But my code version is more universal to cover your evolving and well hidden moving goals. :D

Also, consider renaming this forum thread to "Conditionally move disk content" or such to help others with similar task to find it.

tweacle
Posts: 71
Joined: 11 Apr 2018 05:38

Conditionally move disk content

#13 Post by tweacle » 02 Jun 2018 12:08

Thanks for boost but I think that im not totally compitent yet by a long way.

What I need is after all the files that have moved that have a prefix "B" I need the rest of the files regardless of name to be moved to Other1. It just downloading into O:\Temp\Other and then need to save into O:\Temp\Other\btp if letter starts with "B" and O:\Temp\Other\Other1 for everything else.

Im thinking like this as second one?? I think I would be OK running as 2 seperate items but trying to get to run as one.

Code: Select all

@echo off
setlocal
set "source=O:\Temp\Other" & set "dest=O:\Temp\Other\Other1"  
for /d %%B in ("%dest%") do set "fold=%%~nxB"
for /f "tokens=*" %%a in ('dir /b "%source%\%prefix%*"') do ( echo Moving "%%a"
	if exist "%source%\%%a\" ( xcopy "%source%\%%a" "%dest%\%%a" /s /h /k /c /y /i & if not %%a==%fold% rd "%source%\%%a" /q /s 
	) else ( xcopy "%source%\%%a" "%dest%\Files\%%a*" /c /y /i & del %source%\%%a ) 
)
endlocal

MarioZac
Posts: 38
Joined: 16 May 2018 00:12

Re: Need help in amending a script.

#14 Post by MarioZac » 02 Jun 2018 21:59

tweacle wrote:
02 Jun 2018 12:08
im not totally compitent yet by a long way.
Never will be if not trying to do things yourself. Do you need to move only remaining files to Other1, or also remaining folders somewhere else? Your appetite for moving things grows by an hour. :D

To change the topic title, you need to edit the 1st post title.

tweacle
Posts: 71
Joined: 11 Apr 2018 05:38

Re: Need help in amending a script.

#15 Post by tweacle » 03 Jun 2018 02:10

Aaaahhhh right. I need to move remaining folders

Post Reply