1

I have about 1000 files like:

_etc_sec _home_host_www_temp and etc. 

How to make batch files to make folder inside each other depending on it filenames and then put this file into it.

All files are on d:\ So if we have file named _etc_sec we must create folder named d:\etc then put file _etc_sec into file d:\etc

So if we have file named _home_host_www_temp we must create folder named d:\home then inside this folder create folder host and then inside folder host create folder www then put file _home_host_www_temp into file d:\home\host\www

I know how do that with VBA but batch cmd is to difficult for me. Thanks.

3
  • 1
    You can't do what you are asking in any language on a Windows platform because Windows does not allow / in a file name. The / character is an alternative to the \ folder delimiter, though it is not always reliable in Windows. Commented Aug 12, 2012 at 17:40
  • If you know how to do this in VBA, why don't you use VBScript for this? Has almost the same syntax as VBA and can be run via the cscript command directly in the cmd shell. Commented Aug 12, 2012 at 17:45
  • Because i use VBA from Excel. it is not comfortable and need more system resources to execute the code. CMD Batch more simple using but difficult to writing. Commented Aug 12, 2012 at 17:51

1 Answer 1

1

Use SET search and replace to change _ into \, then use FOR the variable ~p modifier to get the full path that needs to be created. A single MKDIR command can make a series of nested directories. I toggle delayed expansion on and off within the loop just in case you have a filename with an ! character.

@echo off setlocal disableDelayedExpansion for %%F in (*_*) do ( set "file=%%F" setlocal enableDelayedExpansion for %%A in ("!file:_=\!") do ( endlocal 2>nul mkdir "%%~pA" >nul move "%%F" "%%~pA" ) ) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.