Ceate Folder from Filename, copy and Move

November 18, 2010 at 12:51:16
Specs: Window XP
Hi, hope someone can help.

I'd like to create a batch file called KDO1 and add it to the "send to" context menu.

When I select an .exe file and chose "send to" "KDO1", it will do the following:

1. create a folder named after the file (without the .exe extension)
2. copy the file to the new folder
3. move the new folder and copied file to C:\KDO1

I then need a second batch file called "KDO2" to do the same thing but would move the new folder and copied file to C:\KDO2

File names are all 5 digits, some with a letter suffix eg 12345.exe or 12345a.exe

I know how to add a batch file to the "send to" menu.

It would be great if someone can help.

A million thanks,
soup


See More: Ceate Folder from Filename, copy and Move

Report •

#1
November 19, 2010 at 10:57:10

for /f "tokens=1 delims=." %%a in ("%1") do set file=%%a

set file = %1
echo %file%
md D:\temp\%file%
copy %1 D:\temp\%file%\


1st line of code will remove the .exe extension of the file..To remove the folder path from the %1 variable i need to know whether all the files will be in same folder or on random position.

Rest of lines will create the folder with the file name without extention and copy the file to thatfolder.

Subhash Chandra.


Report •

#2
November 19, 2010 at 11:44:20
below code does extactly the same you mentioned.

@echo off
echo file name = %~n1
echo %~dp1%~n1
echo creating folder
md %~dp1%~n1
echo.
echo Coping the file to new folder.
copy %1 %~dp1%~n1\
echo moving the folder to C:\KD01
move %~dp1%~n1 C:\KD01\


for your 2nd batch file, just replace C:\KD01 to C:\kd02 and it will work.

* It will work with any file name with any extention.

Subhash Chandra.


Report •

#3
January 7, 2011 at 01:56:13
Hi Subhash,

This works fantastically, thanks for your time, you have made our life much better.

Often we have the need to move a number of files to the folder.
Example:
12345.exe
12345.dat

We find that if the folder already exists, then the action fails. If we highlight 2 files, only the first one is completed. In other words, we can only use the batch file to copy one file over to the newly created folder.
What we have is great, thanks. In a perfect world, we would like to highlight all the files with the same file numbers (different extensions) and have them all copy over in one action to the one created folder.

Sometimes we work on a number of different file names together, it would be even sweeter if we could select all of the files and then the correct folders are created and files moved to the relevant folder in one action, but maybe this is too much for a batch file?

Example:
12345.exe
12345.dat
54321.exe
54321.pas

Thanks again for your time,
soup


Report •

Related Solutions

#4
January 9, 2011 at 13:13:22
You welcome :), Happy to help.

I created a batch file..YOu have to put it in the folder where the files are..
below is what it will do.

1) It will ask you if you want to process all files or some of the files.

2) if you choose all files, next it will ask you whether you want to move the files to C:\KD01\ or copy them.

3) If you selec MOVE then it will move all the files ,same way COPY will copy the files to C:\kd01\

4) In step one if you choose few files (Type N for that) it will ask you to enter part of the file names, (you said- we would like to highlight all the files with the same file numbers ) ,it will show you all the files with that pattern and press any key to continue

5) Now, it will create folders for each file files without extension and then copy/move the files to that location.


Try it and let me know if any bugs :). I have tested on my system with those 5 files you mentioned and its working.

Here it goes .

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@echo off && Color a && cls

::1. create a folder named after the file (without the .exe extension)
::2. copy the file to the new folder
::3. move the new folder and copied file to C:\KDO1
set _CH=
Title Copy/move files to directory.
:Ask
echo.
echo *********************************************************
echo * This Batch script will copy / move all or set of files*
echo * to their recpective folders... *
echo *********************************************************
echo.
echo Y will process all the files in this folder.
echo N will ask you to enter part of the file name set
echo.
set /p _CH=Would you like to run this batch for all the files in this folder (Y/N) :
if /i %_CH% == Y goto _All
if /i %_CH% == N goto _Sel
goto :EOF

:_All
dir /b | findstr /i /v "mmm.bat" | findstr /i /v "filelist.txt">filelist.txt
goto Action

:_Sel
cls
echo.
echo.
echo Please enter part of the file names which you want to process.
echo Example 1234 or 12345 or 5432 etc
Echo.
set /p _File= Please type :
dir /b *%_FILE%* | findstr /i /v "mmm.bat" | findstr /i /v "filelist.txt">filelist.txt
Goto Action


:Action_Copy
for /f %%a in (filelist.txt) do (
md c:\kd01\%%~na
cls
echo processing file %%a
copy %%a c:\kd01\%%~na\%%a

)
cls
echo files copied successfully.
erase filelist.txt
goto :EOF


:Action_Move
for /f %%a in (filelist.txt) do (
md c:\kd01\%%~na
cls
echo processing file %a
move %%a c:\kd01\%%~na\%%a
erase filelist.txt
)
cls
echo Files moved successfully.
goto :EOF

:Action
cls
type filelist.txt
echo.
echo.
Echo Above are the files which are going to be proceesed..
Pause
cls
echo.
echo.
echo Please select whether you want to move the files or copy the files.
echo.
echo Move will move all the files to C:\KD01\ folder name \filename
echo.
echo Copy will copy the files to C:\KD01\ foldername and file name
echo.
set /p _CPMV= Enter MOVE or COPY and Press Enter. :
if /i %_CPMV% == MOVE goto Action_Move
if /i %_CPMV% == copy goto Action_Copy

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

THis may have some wordwraps, so if you want you can download it from
http://www.chandrasubhash.co.cc/sou...


Thank you.
Subhash Chandra.

Subhash Chandra.


Report •

Ask Question