PoshCode Archive  Hex Artifact Content

Artifact 0c704f65dd6e6f79f81026332e6c387b2f339bbabada1b7dd7b911dbedbe5b32:

  • File Renaming-folder-names.ps1 — part of check-in [909ba69e57] at 2018-06-10 14:07:49 on branch trunk — I just want to rename the folder names in particular path where folder name starts with keep all characters by dropping if “12_” before the file name. I am able to rename it but not able to get log file for the following script. (user: Srinu size: 999)

0000: 23 20 65 6e 63 6f 64 69 6e 67 3a 20 61 73 63 69  # encoding: asci
0010: 69 0d 0a 23 20 61 70 69 3a 20 70 6f 77 65 72 73  i..# api: powers
0020: 68 65 6c 6c 0d 0a 23 20 74 69 74 6c 65 3a 20 52  hell..# title: R
0030: 65 6e 61 6d 69 6e 67 20 66 6f 6c 64 65 72 20 6e  enaming folder n
0040: 61 6d 65 73 0d 0a 23 20 64 65 73 63 72 69 70 74  ames..# descript
0050: 69 6f 6e 3a 20 49 20 6a 75 73 74 20 77 61 6e 74  ion: I just want
0060: 20 74 6f 20 72 65 6e 61 6d 65 20 74 68 65 20 66   to rename the f
0070: 6f 6c 64 65 72 20 6e 61 6d 65 73 20 69 6e 20 70  older names in p
0080: 61 72 74 69 63 75 6c 61 72 20 70 61 74 68 20 77  articular path w
0090: 68 65 72 65 20 66 6f 6c 64 65 72 20 6e 61 6d 65  here folder name
00a0: 20 73 74 61 72 74 73 20 77 69 74 68 20 6b 65 65   starts with kee
00b0: 70 20 61 6c 6c 20 63 68 61 72 61 63 74 65 72 73  p all characters
00c0: 20 62 79 20 64 72 6f 70 70 69 6e 67 20 69 66 20   by dropping if 
00d0: e2 80 9c 31 32 5f e2 80 9d 20 62 65 66 6f 72 65  ...12_... before
00e0: 20 74 68 65 20 66 69 6c 65 20 6e 61 6d 65 2e 20   the file name. 
00f0: 49 20 61 6d 20 61 62 6c 65 20 74 6f 20 72 65 6e  I am able to ren
0100: 61 6d 65 20 69 74 20 62 75 74 20 6e 6f 74 20 61  ame it but not a
0110: 62 6c 65 20 74 6f 20 67 65 74 20 6c 6f 67 20 66  ble to get log f
0120: 69 6c 65 20 66 6f 72 20 74 68 65 20 66 6f 6c 6c  ile for the foll
0130: 6f 77 69 6e 67 20 73 63 72 69 70 74 2e 0d 0a 23  owing script...#
0140: 20 76 65 72 73 69 6f 6e 3a 20 30 2e 31 0d 0a 23   version: 0.1..#
0150: 20 74 79 70 65 3a 20 73 63 72 69 70 74 0d 0a 23   type: script..#
0160: 20 61 75 74 68 6f 72 3a 20 53 72 69 6e 75 0d 0a   author: Srinu..
0170: 23 20 6c 69 63 65 6e 73 65 3a 20 43 43 30 0d 0a  # license: CC0..
0180: 23 20 78 2d 70 6f 73 68 63 6f 64 65 2d 69 64 3a  # x-poshcode-id:
0190: 20 36 30 38 38 0d 0a 23 20 78 2d 61 72 63 68 69   6088..# x-archi
01a0: 76 65 64 3a 20 32 30 31 35 2d 31 31 2d 31 33 54  ved: 2015-11-13T
01b0: 32 33 3a 34 33 3a 33 39 0d 0a 23 20 78 2d 70 75  23:43:39..# x-pu
01c0: 62 6c 69 73 68 65 64 3a 20 32 30 31 35 2d 31 31  blished: 2015-11
01d0: 2d 31 32 54 31 37 3a 30 36 3a 30 30 0d 0a 23 0d  -12T17:06:00..#.
01e0: 0a 23 0d 0a 24 46 6f 6c 64 65 72 73 20 3d 20 47  .#..$Folders = G
01f0: 65 74 2d 43 68 69 6c 64 49 74 65 6d 20 27 45 3a  et-ChildItem 'E:
0200: 5c 52 6f 6f 74 20 46 6f 6c 64 65 72 5c 31 32 5f  \Root Folder\12_
0210: 2a 27 0d 0a 0d 0a 24 63 6f 6c 6c 65 63 74 69 6f  *'....$collectio
0220: 6e 20 3d 20 24 28 29 0d 0a 46 6f 72 65 61 63 68  n = $()..Foreach
0230: 20 28 24 46 6f 6c 64 65 72 20 69 6e 20 24 46 6f   ($Folder in $Fo
0240: 6c 64 65 72 73 29 0d 0a 0d 0a 7b 0d 0a 0d 0a 24  lders)....{....$
0250: 73 74 61 74 75 73 20 3d 20 40 7b 20 22 4f 6c 64  status = @{ "Old
0260: 46 6f 6c 64 65 72 4e 61 6d 65 22 20 3d 20 24 46  FolderName" = $F
0270: 6f 6c 64 65 72 2e 4e 61 6d 65 3b 20 22 4e 65 77  older.Name; "New
0280: 46 6f 6c 64 65 72 4e 61 6d 65 22 20 3d 20 24 46  FolderName" = $F
0290: 6f 6c 64 65 72 2e 4e 61 6d 65 2e 53 75 62 73 74  older.Name.Subst
02a0: 72 69 6e 67 28 33 29 3b 20 22 54 69 6d 65 53 74  ring(3); "TimeSt
02b0: 61 6d 70 22 20 3d 20 28 47 65 74 2d 44 61 74 65  amp" = (Get-Date
02c0: 20 2d 66 20 79 79 79 79 3a 4d 4d 3a 64 64 2d 68   -f yyyy:MM:dd-h
02d0: 68 3a 6d 6d 3a 73 73 29 20 7d 0d 0a 0d 0a 24 4e  h:mm:ss) }....$N
02e0: 65 77 46 6f 6c 64 65 72 4e 61 6d 65 20 3d 20 24  ewFolderName = $
02f0: 46 6f 6c 64 65 72 2e 4e 61 6d 65 2e 53 75 62 73  Folder.Name.Subs
0300: 74 72 69 6e 67 28 33 29 3b 20 0d 0a 52 65 6e 61  tring(3); ..Rena
0310: 6d 65 2d 49 74 65 6d 20 2d 50 61 74 68 20 24 46  me-Item -Path $F
0320: 6f 6c 64 65 72 20 2d 4e 65 77 4e 61 6d 65 20 24  older -NewName $
0330: 4e 65 77 46 6f 6c 64 65 72 4e 61 6d 65 0d 0a 0d  NewFolderName...
0340: 0a 4e 65 77 2d 4f 62 6a 65 63 74 20 2d 54 79 70  .New-Object -Typ
0350: 65 4e 61 6d 65 20 50 53 4f 62 6a 65 63 74 20 2d  eName PSObject -
0360: 50 72 6f 70 65 72 74 79 20 24 53 74 61 74 75 73  Property $Status
0370: 20 0d 0a 20 20 0d 0a 7d 0d 0a 0d 0a 24 63 6f 6c   ..  ..}....$col
0380: 6c 65 63 74 69 6f 6e 20 7c 20 4f 75 74 2d 46 69  lection | Out-Fi
0390: 6c 65 20 44 3a 5c 24 28 28 47 65 74 2d 44 61 74  le D:\$((Get-Dat
03a0: 65 29 2e 54 6f 53 74 72 69 6e 67 28 27 79 79 79  e).ToString('yyy
03b0: 79 4d 4d 64 64 27 29 29 5f 52 65 6e 61 6d 69 6e  yMMdd'))_Renamin
03c0: 67 5f 46 6f 6c 64 65 72 5f 4e 61 6d 65 73 5f 73  g_Folder_Names_s
03d0: 63 72 69 70 74 5f 4c 6f 67 2e 74 78 74 20 2d 41  cript_Log.txt -A
03e0: 70 70 65 6e 64 0d 0a                             ppend..