![]() |
|
[Shell] Batch file to move certain files to another folder and creating link to original sour - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Shell] Batch file to move certain files to another folder and creating link to original sour (/showthread.php?tid=5889517) |
Batch file to move certain files to another folder and creating link to original sour - DOSuser2018 - Sep 28, 2018 Hi to all, I would like to create a .bat file that moves certain files (based on their extension) from a folder (with subfolders) to another disk preserving original tree structure while creating a .lnk in the original location to the moved files. For example the .bat would behave like this: File extension: mkv Source path: d:\movies Destination path e:\movies The batch scans folder and subfolders and moves all the .mkv files to the new path, while creating a .lnk in the original source folder for each file that has been moved. I found some code that I adapted to my purpose: @echo off cls setlocal enabledelayedexpansion set SOURCE_DIR=K:\source set DEST_DIR=K:\dest set FILENAMES_TO_COPY=*.txt for /R "%SOURCE_DIR%" %%F IN (%FILENAMES_TO_COPY%) do ( if exist "%%F" ( echo source: %%F set FILE_DIR=%%~dpF set FILE_INTERMEDIATE_DIR=!FILE_DIR:%SOURCE_DIR%=! set FILE_NAME_EXT=%%~nxF set FILE_DIR set FILE_INTERMEDIATE_DIR set FILE_NAME_EXT xcopy /S /I /Y /V "%%F" "%DEST_DIR%!FILE_INTERMEDIATE_DIR!" echo destination "%DEST_DIR%!FILE_INTERMEDIATE_DIR%FILE_NAME_EXT!" pause ) ) In this first attempt, I just copy the files, and it works. I want to check the destination (which I'll use as parameter for mklink) but the last echo echo destination "%DEST_DIR%!FILE_INTERMEDIATE_DIR%FILE_NAME_EXT!" This one gives me destination "K:\dest" I also tried echo destination "%DEST_DIR%!FILE_INTERMEDIATE_DIR!FILE_NAME_EXT!" This one gives me destination "K:\dest\FILE_NAME_EXT" Not what I would like to have, ie the full destination path\filename.extension I guess I'm just missing something trivial... Any ideas would be highly appreciated. Thank you very much, Matt RE: Batch file to move certain files to another folder and creating link to original sour - Lib - Sep 28, 2018 Does this need to be a Batch Script? I Can't imagine any reason to use Batch over Powershell. If it can be made in Powershell, I can help you out. Let me know. RE: Batch file to move certain files to another folder and creating link to original sour - DOSuser2018 - Sep 28, 2018 (Sep 28, 2018 08:26 AM)Psчcho Wrote: Does this need to be a Batch Script? Hi! I wanted to use batch script only because I knew it a (very) little and I don't know at all Powershell, but it's really ok for me, as long as I can create something that does what I need to! :) Thank you! RE: Batch file to move certain files to another folder and creating link to original sour - Lib - Sep 28, 2018 Code $SourcePath = "D:\movies"Please give this a Try. To run it, Search for Powershell ISE, Open it and at the top left, hit the "New Script" Button. Paste the Code in the Top White part of the Shell and Run the entire script using "F5" or the Green Play button at the top. Let me know if it works or not. RE: Batch file to move certain files to another folder and creating link to original sour - DOSuser2018 - Sep 28, 2018 (Sep 28, 2018 11:34 AM)Psчcho Wrote: Thank you very very much!!!!!!!! That's super great!!!! I edited it a bit, to try it on small folders and with .txt files: Code: $SourcePath = "K:\source" $DestinationPath = "k:\source2" $FilesToMove = Get-ChildItem $SourcePath -Filter "*.txt" Foreach ($FileToMove in $FilesToMove) { Move-Item $FileToMove.FullName -Destination $DestinationPath $TargetFile = "$DestinationPath\" + $FileToMove.Name $WScriptShell = New-Object -ComObject WScript.Shell $Shortcut = $WScriptShell.CreateShortcut("$SourcePath\$FileToMove.lnk") $Shortcut.TargetPath = $TargetFile $Shortcut.Save() } I used Powershell ISE (the prompt says v1.0) on my Windows 10 64bit but I get this error: Code: : The term 'Code:' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1 + Code: + + CategoryInfo : ObjectNotFound: (Code::String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException I've seen that Powershell has been updated to v6.1 so I downloaded it and run it. It created a new folder under Programs files, but I just found pwsh.exe and it's a prompt command. Is there a Powershell ISE v6.1 somewhere? Thank you again for your help, you've opened up a new world to me! Matt RE: Batch file to move certain files to another folder and creating link to original sour - Lib - Sep 28, 2018 Looks like you've got it to run perfectly, the "Code:" Portion was added by formatting on Hack forums, Just remove that and the script should run fine. RE: Batch file to move certain files to another folder and creating link to original sour - DOSuser2018 - Sep 28, 2018 (Sep 28, 2018 11:56 AM)Psчcho Wrote: Looks like you've got it to run perfectly, the "Code:" Portion was added by formatting on Hack forums, Just remove that and the script should run fine. Hi! I removed "Code:", typed set-executionpolicy unrestricted and the script run, but not as expected. I run it on a temp folder with some text files. The script didn't copy the source structure (it just created the destination folder with just on txt file) and strangely it also corrupted the fist "info.txt" file in the source folder (it was a 0-byte empty file): L À F‹ …?g¾WÔ…?g¾WÔ…?g¾WÔY ß PàOÐ ê:i¢Ø +00 /K:\ V 1 =MM4 source2 @ ï¾=M94=MM4. øZ Õ!Þ s o u r c e 2 Z 2 Y =M@4 info.txt B ï¾=M@4=M@4. [ {v´ i n f o . t x t F 1 E ÇÈ^ì DATA K:\source2\info.txt . . \ s o u r c e 2 \ i n f o . t x t ` X desktop-kp2oqgm Œ- h°ËA¦p²ÚÊ…öÌÔä–¯Ãè´FØËŠ| &Œ- h°ËA¦p²ÚÊ…öÌÔä–¯Ãè´FØËŠ| &E 9 1SPS±mDpH§H@.¤=xŒ h H ¿l3Ü This is the content of the only copied file: L À F‰ ß PàOÐ ê:i¢Ø +00 /K:\ V 1 source2 @ ï¾ . s o u r c e 2 Z 2 info.txt B ï¾ . i n f o . t x t . . \ s o u r c e 2 \ i n f o . t x t I would like to attach a zip file with the source folder, but I don't know how to attach it. Thanks again, really! Matt RE: Batch file to move certain files to another folder and creating link to original sour - Lib - Sep 29, 2018 (Sep 28, 2018 11:51 PM)DOSuser2018 Wrote: Hi! Could you please send me a few Screenshots of what you mean. Currently, The script will look in The folder called 'Movies' for Files only, There's no Recursiveness, If there are sub Folders inside 'Movies' you'd like to keep, Let me know. Ideally, Please PM me a screenshot of your file structure if this is the case. I'm not quite sure what you mean with attatching a Zip file? The script is exclusively looking for any file that ends in .mkv. RE: Batch file to move certain files to another folder and creating link to original sour - DOSuser2018 - Sep 29, 2018 (Sep 29, 2018 06:19 AM)Psчcho Wrote: Could you please send me a few Screenshots of what you mean. Hi, and thank you again for your time, I really appreciate that!!!! The script I need must use recursiveness for all the subfolders and just move certain files, for now and for the sake of testing, .txt files. Other files than .txt files must not be touched. By attaching a zip file (just 2 kb actually) I mean I would like to send you the source folder I'm testing your script on, so that you can have my same results. I can't send a PM because first I need 25 quality posts to send a PM. When I post a message, like this reply to you, I don't see the option to send the forum some files, am I missing something? I would like to send the zip file and some screen shots. Thanks again, Matt RE: Batch file to move certain files to another folder and creating link to original sour - Lib - Sep 29, 2018 (Sep 29, 2018 09:47 AM)DOSuser2018 Wrote: Hi, and thank you again for your time, I really appreciate that!!!! It's all good man, I'm enjoying it ;) Just send me a PM with an uploaded screenshot using imgur or something. Recursiveness is super easy in powershell, just finding it hard to Visualize what your File system is like :) RE: Batch file to move certain files to another folder and creating link to original sour - DOSuser2018 - Sep 30, 2018 (Sep 29, 2018 01:01 PM)Psчcho Wrote: It's all good man, I'm enjoying it ;) Hi! I uploaded these images here h t t p s://i m g u r .com/a/MWrk3Ps It's just a simple folder with subfolders, randomly created just for testing. The folder has some .txt files and a .doc file. I just want to move the .txt files to another folder on another disk, creating just the folders which actually contain .txt files, creating a link in the original source folder to the new moved file. If the original folder has a subfolder that doesn't contain any .txt files, a sulfolder with this name doesn't have to be copied to the destination folder. This is the folder structure I'm using for testing: K:\source info1.txt source-a (subfolder) info.txt source-a1.txt source-b (subfolder) dontmove.doc <------ note this info.txt source-a1.txt After the script has run, I need to have: e:\destination info1.txt source-a (subfolder) info.txt source-a1.txt source-b (subfolder) info.txt source-a1.txt K:\source info1.LINK TO FILE IN E:\DESTINATION source-a (subfolder) info.LINK TO FILE IN E:\DESTINATION source-a1.LINK TO FILE IN E:\DESTINATION source-b (subfolder) dontmove.doc info.LINK TO FILE IN E:\DESTINATION source-a1.LINK TO FILE IN E:\DESTINATION I hope it's clearer now. :) Thank you! Hi! I uploaded these images here h t t p s://i m g u r .com/a/MWrk3Ps It's just a simple folder with subfolders, randomly created just for testing. The folder has some .txt files and a .doc file. I just want to move the .txt files to another folder on another disk, creating just the folders which actually contain .txt files, creating a link in the original source folder to the new moved file. If the original folder has a subfolder that doesn't contain any .txt files, a sulfolder with this name doesn't have to be copied to the destination folder. This is the folder structure I'm using for testing: K:\source info1.txt source-a (subfolder) info.txt source-a1.txt source-b (subfolder) dontmove.doc <------ note this info.txt source-a1.txt After the script has run, I need to have: e:\destination info1.txt source-a (subfolder) info.txt source-a1.txt source-b (subfolder) info.txt source-a1.txt K:\source info1.LINK TO FILE IN E:\DESTINATION source-a (subfolder) info.LINK TO FILE IN E:\DESTINATION source-a1.LINK TO FILE IN E:\DESTINATION source-b (subfolder) dontmove.doc info.LINK TO FILE IN E:\DESTINATION source-a1.LINK TO FILE IN E:\DESTINATION I hope it's clearer now. :) Thank you! RE: Batch file to move certain files to another folder and creating link to original sour - Lib - Oct 4, 2018 I promise, I haven't forgotten about this. Just been working late, I'm going to try to figure this out. It's exciting! This is harder than I thought :) RE: Batch file to move certain files to another folder and creating link to original sour - DOSuser2018 - Oct 4, 2018 (Oct 4, 2018 06:27 AM)Psчcho Wrote: I promise, I haven't forgotten about this. I think so as well! :) Hi, just got your PM. I read the script it's not finished, so I'm going to wait! Thank you for now!!! RE: Batch file to move certain files to another folder and creating link to original sour - iTHX - Nov 28, 2018 (Oct 4, 2018 06:27 AM)Psчcho Wrote: I promise, I haven't forgotten about this. have you got the source? id like to check out ps as ive always been loyal to batch |