Scripting Help - Set File Date based on Folder Name (or How to Get Help)

%3CLINGO-SUB%20id%3D%22lingo-sub-2281870%22%20slang%3D%22en-US%22%3EScripting%20Help%20-%20Set%20File%20Date%20based%20on%20Folder%20Name%20(or%20How%20to%20Get%20Help)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2281870%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Community%20-%20I%20need%20help%20and%20hoping%20this%20is%20the%20place%20for%20it.%26nbsp%3B%20Or%20if%20you%20can%20recommend%20where%20I%20should%20ask%20for%20this%20help%2C%20I%20would%20appreciate%20it.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EObstacle%3A%26nbsp%3B%20I%20have%20many%20files%20that%20I%20need%20the%20date%20changed%20to%20the%20name%20of%20the%20folder%20they%20exist%20within.%26nbsp%3B%20I%20would%20like%20a%20script%20that%20would%20cycle%20through%20each%20folder%20and%20do%20this%20automatically.%26nbsp%3B%20%26nbsp%3BLet%20me%20know%20what%20can%20be%20done%20and%20am%20open%20to%20suggestions.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22JCombs_0-1619120166188.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F274795iB6A96A4A647B1DA7%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22JCombs_0-1619120166188.png%22%20alt%3D%22JCombs_0-1619120166188.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2281870%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2281976%22%20slang%3D%22en-US%22%3ERe%3A%20Scripting%20Help%20-%20Set%20File%20Date%20based%20on%20Folder%20Name%20(or%20How%20to%20Get%20Help)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2281976%22%20slang%3D%22en-US%22%3E%3CP%3EHello%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1034221%22%20target%3D%22_blank%22%3E%40JCombs%3C%2FA%3E%2C%3C%2FP%3E%3CP%3EI%20think%20%3CA%20href%3D%22https%3A%2F%2Fstackoverflow.com%2Fquestions%2F56211626%2Fpowershell-change-file-date-created-and-date-modified-based-on-filename%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ethis%3C%2FA%3E%20should%20give%20you%20some%20ideas%20to%20start%20from.%20There%20they%20are%20getting%20date%20from%20file%20name%20and%20in%20your%20case%20you%20need%20to%20do%20it%20from%20parent%20folder.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHope%20that%20helps.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2295826%22%20slang%3D%22en-US%22%3ERe%3A%20Scripting%20Help%20-%20Set%20File%20Date%20based%20on%20Folder%20Name%20(or%20How%20to%20Get%20Help)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2295826%22%20slang%3D%22en-US%22%3Eyou%20can%20you%20the%20below%201%20liner%20to%20grab%20the%20folder%20name%20and%20load%20into%20a%20variable%3CBR%20%2F%3E%24test%20%3D%20%22C%3A%5CTemp%5C2005-12-15%5CFH0000013.jpg%22%3CBR%20%2F%3E%24test.split(%22%5C%22)%5B-1%5D%20will%20return%20FH0000013.jpg%3CBR%20%2F%3E%24test.split(%22%5C%22)%5B-2%5D%20will%20return%202005-12-15%20being%20the%20dated%20folder%20you%20are%20wanting%3CBR%20%2F%3E%3CBR%20%2F%3Estore%20it%20into%20variable%20you%20can%20then%20reuse%20in%20the%20rename%3CBR%20%2F%3E%24fdate%20%3D%20%24test.split(%22%5C%22)%5B-2%5D%3C%2FLINGO-BODY%3E
Occasional Visitor

Hi Community - I need help and hoping this is the place for it.  Or if you can recommend where I should ask for this help, I would appreciate it.

 

Obstacle:  I have many files that I need the date changed to the name of the folder they exist within.  I would like a script that would cycle through each folder and do this automatically.   Let me know what can be done and am open to suggestions.

 

JCombs_0-1619120166188.png

 

2 Replies

Hello @JCombs,

I think this should give you some ideas to start from. There they are getting date from file name and in your case you need to do it from parent folder.

 

Hope that helps.

you can you the below 1 liner to grab the folder name and load into a variable
$test = "C:\Temp\2005-12-15\FH0000013.jpg"
$test.split("\")[-1] will return FH0000013.jpg
$test.split("\")[-2] will return 2005-12-15 being the dated folder you are wanting

store it into variable you can then reuse in the rename
$fdate = $test.split("\")[-2]