-2

I need to move files from one Dir to another while placing the files in sub-directorates according to their file name.

Background:

File name (building Number)-assign-flr-pln.dgn

new location would be F:/Assignment Floor Plans/Buildings/(Building Number)/floor plan file.

The batch files needs to read the .dgn name for the building number then place the file in the corresponding floor plan sub folder in the building number folder

so...

take (building number)-assign-flr-pln.dgn file from one dir and place it.....

Assignment Floor Plans - Buildings -(building number) -Floor Plan Files <-- Here

  • Must it be written in batch? Does your system have any scripting languages installed such as python, php, or does your system have the windows 10 bash shell? Batch is just a terrible way to write things, its confusing to write/debug and doesn't really make much sense. Do you have a C# compiler on that machine? Just about any solution is better than the batch-based one. – HumbleWebDev Aug 29 '17 at 15:10
  • 1
    Is the building number always in parentheses? and are you intending to name directories to `(building number)` also including parentheses? Also, you should be aware that asking a coding question, without showing your code is off topic here, please [edit](https://stackoverflow.com/posts/45942579/edit) your post to include the code you are having trouble with, explaining exactly what the trouble is with it. – Compo Aug 29 '17 at 15:16
  • sorry, I'm new to Stackoverflow, I do not have the code I am requesting someone help me with it. However to anwser your question the paren is just a placeholder. Unholy Programmer- using a work machine we only use CMD. So a file names 100-assig-flr-plan.dgn needs to go in the floorplan sub folder of the 100 folder. – Chris VanDyke Aug 29 '17 at 15:47
  • If you "do not have the code" you are not "requesting someone help" you "with it" you are asking someone to do it for you, which as I said is Off Topic. – Compo Aug 29 '17 at 16:53

2 Answers2

0

You have a fair bit to learn if you want to do something this complicated directly from a batch file.

Research list:

FOR
SET
CALL

You'll want to use the FOR command to get the filename; pay attention to the %~ options. For example:

set BAT_DGNFNM=
for %%F in (*.dgn) do set BAT_DGNFNM=%%~nF

This causes the environment variable BAT_DGNFNM to be set to the base filename. However, it loops through all the files, so really, you need it to call a subroutine for each file:

for %%F in (*.dgn) do call :DODGN "%%~dpnxF" "%%~nF"

This calls the subroutine :DODGN and passes it two quote-encapsulated parameters; the first one is the fully-qualified filename and the second one is simply the base filename.

Then you do the actual file handling in the subroutine:

:DODGN
set BAT_DGNFNM=%1
set BAT_DGNBNM=%2
REM Do stuff with BAT_DGNBNM using SET commands
GOTO :EOF

You'll need to make sure you don't "fall through" to the subroutine after the main loop is done, and you should clean up your environment variables when you're done, so this would look something like:

@echo off
for %%F in (*.dgn) do call :DODGN "%%~dpnxF" "%%~nF"
goto ENDIT

:DODGN
set BAT_DGNFNM=%1
set BAT_DGNBNM=%2
REM Do stuff with BAT_DGNBNM using SET commands
GOTO :EOF

:ENDIT
set BAT_DGNFNM=
set BAT_DGNBNM=

Good luck -- this stuff can get complicated.

Final reminder: Most of your learning curve will be with the SET command -- that's how you extract substrings out of one environment variable and put it into another, perform text replacement, etc.

This stuff is way easier in VBS or PowerShell.

0

Although you showed no code we could help you with...

It's quite easy if you know the commands to use (a great site for starters: SS64):

Use a plain for loop to get each file.
Use a for /f loop to extract the building-nr (according to your comment it's the first token separated by a dash)
Use md to create the destination folder (ignore error if already existent)
Use copy to copy the file.
Finished.

@echo off
REM prepare some files for testing:
break>100-assign-flr-pln.dgn
break>110-assign-flr-pln.dgn
break>235-assign-flr-pln.dgn

REM now copy them into their destination folders:
set "dest=Assignment Floor Plans - Buildings - @ - Floor Plan Files"
for %%f in (*-assign-flr-pln.dgn) do (
  for /f "tokens=1 delims=-" %%a in ("%%f") do (
    call md "%%dest:@=%%a%%" 2>nul
    call copy "%%f" "%%dest:@=%%a%%\"
  )
)

REM now show the result:
tree /f

Note: adapt your destination (add F:\)

"Special Effects":
call <command>: introduce another layer of parsing to avoid delayed expansion
@=%%aString replacing within a variable
2>nul suppress error messages

Stephan
  • 53,940
  • 10
  • 58
  • 91